Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Monadic Approach To Ruby Error Handling

Huy Du
December 16, 2023

Monadic Approach To Ruby Error Handling

Huy Du

December 16, 2023
Tweet

More Decks by Huy Du

Other Decks in Programming

Transcript

  1. MONADIC APPROACH TO RUBY ERROR HANDLING Huy Du | exponentdev.com

    RubyConf Taiwan Taipei, 17th December 2023
  2. 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
  3. 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
  4. 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
  5. As a User, I want to buy a Ticket for

    attending Ruby Conference. EXAMPLE PROBLEM
  6. User Frontend API Server Database Payment Gateway Infrastructure HIGH LEVEL

    SYSTEM DESIGN Buy a ticket Send API request Store data Submit payment
  7. NAIVE IMPLEMENTATION Ticket not found? User not found? Order created?

    User buy ticket Insufficient balance? Payment success? NO NO YES YES Render success YES
  8. 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
  9. Can we make it better? NAIVE IMPLEMENTATION • Chaotic •

    Error Prone • Poor extendibility • Poor readability • Not reusable
  10. 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.
  11. 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.
  12. 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
  13. UNEXPECTED ERROR Errors are not expected to occur. They are

    interrupted our program or also known as bugs. • Database corrupted • Memory exceeded • Developer mistake
  14. As a User, I want to buy a Ticket for

    attending Ruby Conference. EXAMPLE PROBLEM
  15. 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
  16. 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
  17. 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
  18. Can we improve it? REFACTOR V1 • Better code flow

    • Poor extendibility • Better readability • Not reusable • Poor encapsulation
  19. 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
  20. 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
  21. SERVICE OBJECT Service Object are Ruby objects, that are designed

    to execute one single action in the business logic, and do it well.
  22. 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
  23. MONADIC HANDLING User buy ticket Create Order failed? Render Error

    Checkout Order failed? NO Render success YES NO YES Render Error
  24. 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
  25. 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
  26. 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.
  27. 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.
  28. 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?
  29. Huy Du ”The correct way of error handling will stop

    your lifespan for a couple of seconds."
  30. 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