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
Error Handling Without Exceptions
Search
Ben Darfler
May 06, 2015
Programming
0
130
Error Handling Without Exceptions
A quick jaunt through functional error handling patterns using Ruby
Ben Darfler
May 06, 2015
Tweet
Share
More Decks by Ben Darfler
See All by Ben Darfler
Self-healing Software
bdarfler
0
120
Microservice Architectures
bdarfler
0
74
dotfiles
bdarfler
0
87
Remote Work
bdarfler
0
180
Low Latency Systems: How do they do it?
bdarfler
0
55
Scala
bdarfler
0
120
HTTP/2
bdarfler
0
120
Rails Asset Pipeline and You
bdarfler
0
50
How to Win at the Internet
bdarfler
1
30
Other Decks in Programming
See All in Programming
見た目から始める生産性向上
ikumatadokoro
7
800
スクラムガイドのスプリントレトロスペクティブを改めて読みかえしてみた / Re-reading the Sprint Retrospective Section in the Scrum Guide
mackey0225
3
410
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
290
Snowflakeで眠ったデータを起こそう!
estie
0
110
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
230
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
700
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
640
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
Git Lint
bkuhlmann
4
750
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
GitHub Copilotのススメ
marcy731
1
190
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
289
19k
Creatively Recalculating Your Daily Design Routine
revolveconf
210
11k
Optimizing for Happiness
mojombo
370
69k
Building Effective Engineering Teams - LeadDev
addyosmani
28
1.8k
Navigating Team Friction
lara
178
13k
Imperfection Machines: The Place of Print at Facebook
scottboms
260
12k
Building an army of robots
kneath
300
41k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
Thoughts on Productivity
jonyablonski
58
3.8k
GraphQLとの向き合い方2022年版
quramy
32
12k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
17
1.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
Transcript
Error Handling Without Exceptions
A Guiding Example ['1','2','3'].map{ |x| Integer(x) }.reduce(:+)
A Guiding Example ['1','2','3'].map{ |x| Integer(x) }.reduce(:+) => 6
A Guiding Example ['1','2','3'].map{ |x| Integer(x) }.reduce(:+) => 6 ['1','2','3','zero'].map{
|x| Integer(x) }.reduce(:+)
A Guiding Example ['1','2','3'].map{ |x| Integer(x) }.reduce(:+) => 6 ['1','2','3','zero'].map{
|x| Integer(x) }.reduce(:+) ArgumentError: invalid value for Integer(): "zero"
“ Errors aren’t exceptions because they’re not exceptional - they’re
real possible outcomes with their own information - treat them as such - Some Guy I Know
Can We Do Better?
Can We Do Better? def safe_to_i(value) Integer(value) rescue nil end
Can We Do Better? def safe_to_i(value) Integer(value) rescue nil end
['1','2','3','zero'].map{ |x| safe_to_i(x) }
Can We Do Better? def safe_to_i(value) Integer(value) rescue nil end
['1','2','3','zero'].map{ |x| safe_to_i(x) } => [1, 2, 3, nil]
Can We Do Better? def safe_to_i(value) Integer(value) rescue nil end
['1','2','3','zero'].map{ |x| safe_to_i(x) } => [1, 2, 3, nil] ['1','2','3','zero'].map{ |x| safe_to_i(x) }.reduce(:+)
Can We Do Better? def safe_to_i(value) Integer(value) rescue nil end
['1','2','3','zero'].map{ |x| safe_to_i(x) } => [1, 2, 3, nil] ['1','2','3','zero'].map{ |x| safe_to_i(x) }.reduce(:+) TypeError: nil can't be coerced into Fixnum
Take Three
Take Three def safer_to_i(value) [Integer(value)] rescue [] end
Take Three def safer_to_i(value) [Integer(value)] rescue [] end ['1','2','3','zero'].map{ |x|
safer_to_i(x) }
Take Three def safer_to_i(value) [Integer(value)] rescue [] end ['1','2','3','zero'].map{ |x|
safer_to_i(x) } => [[1], [2], [3], []]
Take Three def safer_to_i(value) [Integer(value)] rescue [] end ['1','2','3','zero'].map{ |x|
safer_to_i(x) } => [[1], [2], [3], []] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.flatten
Take Three def safer_to_i(value) [Integer(value)] rescue [] end ['1','2','3','zero'].map{ |x|
safer_to_i(x) } => [[1], [2], [3], []] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.flatten => [1, 2, 3]
Take Three def safer_to_i(value) [Integer(value)] rescue [] end ['1','2','3','zero'].map{ |x|
safer_to_i(x) } => [[1], [2], [3], []] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.flatten => [1, 2, 3] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.flatten.reduce(:+)
Take Three def safer_to_i(value) [Integer(value)] rescue [] end ['1','2','3','zero'].map{ |x|
safer_to_i(x) } => [[1], [2], [3], []] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.flatten => [1, 2, 3] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.flatten.reduce(:+) => 6
But I Wanted To Fail!
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end ['1','2','3'].map{ |x| safer_to_i(x) } => [[1], [2], [3]]
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end ['1','2','3'].map{ |x| safer_to_i(x) } => [[1], [2], [3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end ['1','2','3'].map{ |x| safer_to_i(x) } => [[1], [2], [3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence => [[1, 2, 3]]
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end ['1','2','3','zero'].map{ |x| safer_to_i(x) } => [[1], [2], [3], []]
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end ['1','2','3','zero'].map{ |x| safer_to_i(x) } => [[1], [2], [3], []] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence
But I Wanted To Fail! class Array def sequence self.reduce([[]]){
|a,b| a.flat_map{ |aa| b.map{ |bb| aa << bb }}} end end ['1','2','3','zero'].map{ |x| safer_to_i(x) } => [[1], [2], [3], []] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence => []
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]]
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence.map{ |x| x.reduce(:+) }
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence.map{ |x| x.reduce(:+) } => [6]
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence.map{ |x| x.reduce(:+) } => [6] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence.map{ |x| x.reduce(:+) } => [6] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence => []
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence.map{ |x| x.reduce(:+) } => [6] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence => [] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence .map{ |x| x.reduce(:+) }
But I Wanted To Fail! ['1','2','3'].map{ |x| safer_to_i(x) }.sequence =>
[[1, 2, 3]] ['1','2','3'].map{ |x| safer_to_i(x) }.sequence.map{ |x| x.reduce(:+) } => [6] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence => [] ['1','2','3','zero'].map{ |x| safer_to_i(x) }.sequence .map{ |x| x.reduce(:+) } => []
Where To Next?
Where To Next? def saferToInt(str: String): Option[Int] = try {
Some(str.toInt) } catch { case e: Exception => None } class ListOps[A](list: List[Option[A]]) { def sequence[A](a: List[Option[A]]): Option[List[A]] = a.foldRight[Option[List[A]]](Some(Nil))((a,b)=>a.flatMap(aa=>b.map(bb=>aa::bb))) def sequence: Option[List[A]] = sequence(list) } implicit def listToListOps[A](list: List[Option[A]]) = new ListOps(list)
Where To Next? List("1","2","3").map(saferToInt) res12: List[Option[Int]] = List(Some(1), Some(2), Some(3))
List("1","2","3").map(saferToInt).sequence res13: Option[List[Int]] = Some(List(1, 2, 3)) List("1","2","3").map(saferToInt).sequence.map(_.reduce(_+_)) res14: Option[Int] = Some(6)
Where To Next? List("1","2","3","zero").map(saferToInt) res16: List[Option[Int]] = List(Some(1), Some(2), Some(3),
None) List("1","2","3","zero").map(saferToInt).sequence res17: Option[List[Int]] = None List("1","2","3","zero").map(saferToInt).sequence.map(_.reduce(_+_)) res18: Option[Int] = None
Where To Next? This all works because List and Option
are Monads