AVOIDING DISASTER:
PRACTICAL STRATEGIES FOR
ERROR HANDLING
Huy Du | exponentdev.com
RubyConfTH
7th October, 2023
Slide 2
Slide 2 text
Huy Du
Software Engineer @
@dugiahuy
https://exponentdev.com
Slide 3
Slide 3 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 4
Slide 4 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 5
Slide 5 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 6
Slide 6 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 7
Slide 7 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 8
Slide 8 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 9
Slide 9 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 10
Slide 10 text
As a User, I want to buy a Ticket
for attending Ruby Conference.
EXAMPLE PROBLEM
Slide 11
Slide 11 text
User Frontend API Server Database
Payment Gateway
Infrastructure
HIGH LEVEL SYSTEM DESIGN
Buy a ticket Send API request Interact with data
Submit payment
Slide 12
Slide 12 text
Model
Controller Database
Payment Gateway
APPLICATION COMPONENTS
Application
Slide 13
Slide 13 text
NAIVE IMPLEMENTATION
Ticket not found? User not found?
Order created?
User buy ticket
NO
NO
Slide 14
Slide 14 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 15
Slide 15 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 16
Slide 16 text
Can we make it better?
NAIVE IMPLEMENTATION
• Chaotic
• Error Prone
• Poor extendibility
• Poor readability
• Not reusable
Slide 17
Slide 17 text
Model
Controller
Database
Payment Gateway
BUSINESS LOGIC LAYER
Application
Business Logic
Slide 18
Slide 18 text
CREATE BUSINESS LOGIC SERVICES
Slide 19
Slide 19 text
APPLY TO CONTROLLER
Slide 20
Slide 20 text
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
Slide 21
Slide 21 text
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
Slide 22
Slide 22 text
How to resolve it?
REFACTORING
• Better code flow
• Better extendibility
• Better readability
• Not reusable
• Poor encapsulation
Slide 23
Slide 23 text
RETURN MONADS AS
RESULT
Slide 24
Slide 24 text
MONADS
Monads is structure that wrap return value of
function in monadic way
Slide 25
Slide 25 text
MONAD RESULT
Result
Success, Value
Failure, Error
Slide 26
Slide 26 text
MONAD RESULT IN RUBY
Slide 27
Slide 27 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 28
Slide 28 text
SERVICE OBJECT
Slide 29
Slide 29 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
APPLY MONADIC HANDLING
User buy ticket
Create Order
failed?
Render Error
Checkout Order
failed?
NO
Render success
YES
NO YES
Render Error
Slide 35
Slide 35 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 36
Slide 36 text
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.
Slide 37
Slide 37 text
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
Slide 38
Slide 38 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 39
Slide 39 text
Huy Du
”The correct way of error handling will stop
your lifespan for a couple of seconds."
Slide 40
Slide 40 text
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
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
Huy Du
"Pray is not prevented your system from
crash. You need monadic handling."
Slide 43
Slide 43 text
SALUTE!
Huy Du
https://exponentdev.com/talks
https://github.com/dugiahuy/talks