Slide 1

Slide 1 text

MONADIC APPROACH TO RUBY ERROR HANDLING Huy Du | exponentdev.com RubyConf Taiwan Taipei, 17th December 2023

Slide 2

Slide 2 text

EXCEPTION IN RUBY Exception is an unique data structure representing an error or unexpected condition in the Ruby program. Exception objects carry information about: • Type • Message • Backtrace

Slide 3

Slide 3 text

EXCEPTION IN RUBY Exception is a unique data structure representing an error or unexpected condition in the Ruby program. Exception objects carry information about: • Type • Message • Backtrace

Slide 4

Slide 4 text

EXCEPTION IN RUBY Exception is a unique data structure representing an error or unexpected condition in the Ruby program. Exception objects carry information about: • Type • Message • Backtrace

Slide 5

Slide 5 text

As a User, I want to buy a Ticket for attending Ruby Conference. EXAMPLE PROBLEM

Slide 6

Slide 6 text

User Frontend API Server Database Payment Gateway Infrastructure HIGH LEVEL SYSTEM DESIGN Buy a ticket Send API request Store data Submit payment

Slide 7

Slide 7 text

Model Controller Database Payment Gateway APPLICATION COMPONENTS Application

Slide 8

Slide 8 text

NAIVE IMPLEMENTATION Ticket not found? User not found? Order created? User buy ticket NO NO

Slide 9

Slide 9 text

NAIVE IMPLEMENTATION Ticket not found? User not found? Order created? User buy ticket Insufficient balance? Payment success? NO NO YES YES Render success YES

Slide 10

Slide 10 text

NAIVE IMPLEMENTATION Ticket not found? User not found? Order created? User buy ticket Insufficient balance? Render Error Raise Exception Raise Exception Render Error Payment success? YES YES NO NO NO YES YES Render success Render Error Raise Exception YES Render Error NO NO Raise Exception Render Error

Slide 11

Slide 11 text

Can we make it better? NAIVE IMPLEMENTATION • Chaotic • Error Prone • Poor extendibility • Poor readability • Not reusable

Slide 12

Slide 12 text

Huy Dư Software Engineer @ @dugiahuy https://exponentdev.com

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

PROGRAMMING f(x) = y • f() is solution • x is input • y is expected output Programming, in simple term, involves solving problem by providing instructions to a computer.

Slide 15

Slide 15 text

WHAT ARE ERRORS? Software errors are defined as results in unexpected outcomes from a computer program or cause it to act in ways that were not intended.

Slide 16

Slide 16 text

EXPECTED ERROR These are errors that developers anticipate and expect as part of normal program execution • Part of business logic ⚬ Ticket has been sold out. ⚬ Purchase ticket cannot process after midnight. • External dependencies ⚬ Wrong library usage ⚬ Connection error

Slide 17

Slide 17 text

UNEXPECTED ERROR

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

UNEXPECTED ERROR Errors are not expected to occur. They are interrupted our program or also known as bugs. • Database corrupted • Memory exceeded • Developer mistake

Slide 20

Slide 20 text

As a User, I want to buy a Ticket for attending Ruby Conference. EXAMPLE PROBLEM

Slide 21

Slide 21 text

Model Controller Database Payment Gateway APPLICATION COMPONENTS Application

Slide 22

Slide 22 text

Model Controller Database Payment Gateway BUSINESS LOGIC LAYER Application Business Logic

Slide 23

Slide 23 text

REFACTOR V1

Slide 24

Slide 24 text

REFACTOR V1 Ticket not found? User not found? Order created? User buy ticket Render Error Raise Exception Raise Exception Render Error YES YES NO NO NO Render Error Raise Exception

Slide 25

Slide 25 text

REFACTOR V1 Ticket not found? User not found? Order created? User buy ticket Insufficient balance? Render Error Raise Exception Raise Exception Render Error Payment success? YES YES NO NO NO YES YES Render Error Raise Exception

Slide 26

Slide 26 text

REFACTOR V1 Ticket not found? User not found? Order created? User buy ticket Insufficient balance? Render Error Raise Exception Raise Exception Render Error Payment success? YES YES NO NO NO YES YES Render success Render Error Raise Exception YES Render Error NO NO Raise Exception Render Error

Slide 27

Slide 27 text

Can we improve it? REFACTOR V1 • Better code flow • Poor extendibility • Better readability • Not reusable • Poor encapsulation

Slide 28

Slide 28 text

REFACTOR V2

Slide 29

Slide 29 text

REFACTOR V2

Slide 30

Slide 30 text

REFACTOR V2 User buy ticket Create Order failed? Render Error Checkout Order failed? NO Insufficient balance? Render Error YES NO YES YES NO Payment failed? YES Render Error NO Render success

Slide 31

Slide 31 text

REFACTOR V2 User buy ticket Create Order failed? Render Error Checkout Order failed? NO Insufficient balance? Render Error YES NO YES YES NO Payment failed? YES Render Error NO Render success Leaky error details • Inconsistent return value • No standardize

Slide 32

Slide 32 text

RETURN MONADS AS RESULT

Slide 33

Slide 33 text

MONADS Monads is structure that wrap return value of function in monadic way

Slide 34

Slide 34 text

MONAD RESULT Result Success, Value Failure, Error

Slide 35

Slide 35 text

MONAD RESULT

Slide 36

Slide 36 text

SERVICE OBJECT Service Object are Ruby objects, that are designed to execute one single action in the business logic, and do it well.

Slide 37

Slide 37 text

SERVICE OBJECT

Slide 38

Slide 38 text

MONADIC HANDLING Monadic handling is the combine between Service Object and Monads Result. Monadic handling involves using Monad Result to manage the flow of execution within the Service Object

Slide 39

Slide 39 text

MONADIC HANDLING

Slide 40

Slide 40 text

MONADIC HANDLING Monadic Handling Refactor V2

Slide 41

Slide 41 text

MONADIC HANDLING Monadic Handling Refactor V2

Slide 42

Slide 42 text

MONADIC HANDLING User buy ticket Create Order failed? Render Error Checkout Order failed? NO Render success YES NO YES Render Error

Slide 43

Slide 43 text

COMING TO REALITY

Slide 44

Slide 44 text

MONADIC HANDLING • Provide standardize and consistency way error handling • Explicit and reduce complexity of conditional cases • Making code more robust and maintainable • Completely encapsulate logic and reusable everywhere

Slide 45

Slide 45 text

MONADIC HANDLING GEMS https://github.com/dry-rb/dry-monads https://github.com/kaligo/stimpack

Slide 46

Slide 46 text

ERROR HANDLING f(x) = y Input x must meet the specific criteria of validity to ensure f() can process it properly. • f() is solution • x is input • y is expected output Solution f() is designed to take a valid input and process it into our expected output, y

Slide 47

Slide 47 text

Huy Du ”Error handling is ensuring valid inputs and predictable outputs.”

Slide 48

Slide 48 text

VALIDITY VS EXECUTION f(x) = y Validity errors are directly tied to the appropriateness of the input x. • f() is solution • x is input • y is expected output Execution errors emerge even with a valid input, arising from issues within the function f(x) itself during the execution.

Slide 49

Slide 49 text

MONADIC VS TRADITIONAL Monadic Approach (Validity Errors) Traditional Approach (Execution Errors) They are not being mutually exclusive • Encapsulates and validates input data. • Ensures errors are part of the expected flow. • Ideal for catching issues before they escalate. • Catches unexpected operational issues. • Acts as safety net through ruby exception mechanisms. • Handles disruptions in the program’s flow.

Slide 50

Slide 50 text

BEFORE RAISING EXCEPTION • Is this action truly unexpected? • Am I going to disrupt the execution flow? • Will I have a heart attack when this exception thrown?

Slide 51

Slide 51 text

Huy Du ”The correct way of error handling will stop your lifespan for a couple of seconds."

Slide 52

Slide 52 text

WRAP UP • Understand different type of errors in software application • Understand how Exception in Ruby • Create boundaries for Business Logic layer and encapsulate with Service Object • Use monadic error handling by combining Service Object and Monad Result • You have the practical strategies for error handling

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Huy Du "Pray is not prevented your system from crash. You need monadic handling."

Slide 55

Slide 55 text

SALUTE! Huy Du https://exponentdev.com/talks https://github.com/dugiahuy/talks