Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Using Monads for Elegant Error Handling
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
John Gallagher
October 28, 2021
Programming
0
45
Using Monads for Elegant Error Handling
John Gallagher
October 28, 2021
Tweet
Share
More Decks by John Gallagher
See All by John Gallagher
Fix Production Bugs Quickly - Warsaw Ruby Conference 2024
johngallagher
0
4
Fix Bugs 20x Faster - Belfast Ruby 2025
johngallagher
0
4
Fix Production Bugs 20x Faster - The Wroclave.rb 2025
johngallagher
0
4
Using Monads for Elegant Error Handling - RubyConf 2021
johngallagher
0
4
Fix Production Bugs 20x Faster - Wroclave.rb - 2025
johngallagher
0
4
Squash Production Defects Quickly - The Power Of Structured Logging - EuRuKo 2024
johngallagher
0
4
Other Decks in Programming
See All in Programming
Fundamentals of Software Engineering In the Age of AI
therealdanvega
1
230
AI活用のコスパを最大化する方法
ochtum
0
130
Docコメントで始める簡単ガードレール
keisukeikeda
1
110
Railsの気持ちを考えながらコントローラとビューを整頓する/tidying-rails-controllers-and-views-as-rails-think
moro
4
380
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
220
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.6k
API Platformを活用したPHPによる本格的なWeb API開発 / api-platform-book-intro
ttskch
1
130
Geminiの機能を調べ尽くしてみた
naruyoshimi
0
200
CopilotKit + AG-UIを学ぶ
nearme_tech
PRO
2
150
AIとペアプロして処理時間を97%削減した話 #pyconshizu
kashewnuts
1
210
NOT A HOTEL - 建築や人と融合し、自由を創り出すソフトウェア
not_a_hokuts
2
900
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
250
Featured
See All Featured
Joys of Absence: A Defence of Solitary Play
codingconduct
1
300
Code Review Best Practice
trishagee
74
20k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
What does AI have to do with Human Rights?
axbom
PRO
1
2k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
68
Being A Developer After 40
akosma
91
590k
The Language of Interfaces
destraynor
162
26k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Transcript
Using Monads for Elegant Error Handling John Gallagher
None
None
None
None
None
None
Failure modes? Caught all exceptions? Customer experience?
Example - Address Editing Find contact Lookup postcode* Success? Render
manual entry Render select Success? Render error Yes Yes No No * Postcode - zip code in the US
Exceptions
Exceptions
Exceptions
Problems Returning nil harmful Client knows too much No return
value convention Customers: broken software You: doubt, decision fatigue
What is a Result Monad? Represents success or failure Consistent
interface Wraps value or error
Refactor to Monads
Success and Failure
None
None
Exceptions
Monads
Exceptions
Monads
Exceptions
Monads
Convert to pipeline
Handle success with .fmap
None
None
Handle failure with .or
None
None
Chain .fmap and .or
None
None
Monads
Monads and Pipeline
Exceptions Monads
None
Turn it up to eleven!
Turn it up to eleven!
Benefits Readable pipeline Scales well Helps thinking Failures store errors
Drawbacks More boilerplate code More dependencies Not idiomatic Ruby More
to understand
None
Quiz
Question 1 Answer: C
Question 2 Answer: A
Practice https://bit.ly/eleganterrors Run through the exercise Submit a PR Ask
any questions
Thank you! John Gallagher Lead Developer, BiggerPockets Automation Consultant, Synaptic
Mishap Twitter: @synapticmishap LinkedIn: @synapticmishap Github: @johngallagher
Do notation Try Check out the docs for Dry Monads