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

Avoiding Disaster: Practical Strategies for Err...

Huy Du
October 07, 2023

Avoiding Disaster: Practical Strategies for Error Handling

Huy Du

October 07, 2023
Tweet

More Decks by Huy Du

Other Decks in Programming

Transcript

  1. AVOIDING DISASTER: PRACTICAL STRATEGIES FOR ERROR HANDLING Huy Du |

    exponentdev.com RubyConfTH 7th October, 2023
  2. 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.
  3. 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.
  4. 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
  5. UNEXPECTED ERROR Errors are not expected to occur. They are

    interrupted our program or also known as bugs. • Database corrupted • Memory exceeded • Developer mistake
  6. 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
  7. 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
  8. 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
  9. As a User, I want to buy a Ticket for

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

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

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

    Error Prone • Poor extendibility • Poor readability • Not reusable
  14. CONTROLLER REFACTORING 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
  15. REFACTORING ISSUES 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 • Return inconsistent value • No standardize
  16. How to resolve it? REFACTORING • Better code flow •

    Better extendibility • Better readability • Not reusable • Poor encapsulation
  17. SERVICE OBJECT Service Object are Ruby objects, that are designed

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

    Error Checkout Order failed? NO Render success YES NO YES Render Error
  20. 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
  21. ERROR HANDLING f(x) = y Input value x must meet

    valid criteria to resolve problem. • f() is solution • x is input • y is expected output Solution f() is expected to produce final result without any issues.
  22. VALIDITY VS EXECUTION f(x) = y Validity errors pertain to

    issues related to the input x • f() is solution • x is input • y is expected output Execution errors refer to issues that arise during execution of f() with the valid input x
  23. 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?
  24. Huy Du ”The correct way of error handling will stop

    your lifespan for a couple of seconds."
  25. WRAP UP • Understand different type of errors in software

    application • Understand how exception works 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