-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ktg
More file actions
59 lines (53 loc) · 1.36 KB
/
Copy patherrors.ktg
File metadata and controls
59 lines (53 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
; `attempt` is a pipeline dialect for composing steps that
; can fail. Each step threads the previous result through
; `it`. Errors short-circuit to a matching `catch` arm.
; --- source + then pipeline ---
result: attempt [
source [" Hello, World "]
then [trim it]
then [lowercase it]
then [split it ", "]
]
probe result ; ["hello" "world"]
; --- without source, attempt is a reusable function ---
clean: attempt [
when [not empty? it]
then [trim it]
then [lowercase it]
]
print clean " HEY " ; hey
; --- catch by error kind ---
recover: attempt [
source [error 'parse "bad input"]
catch 'parse [
rejoin ["recovered: " error]
]
fallback ["unknown failure"]
]
print recover ; recovered: bad input
; --- fallback for anything uncaught ---
loud-parse: attempt [
source [error 'other "boom"]
catch 'parse ["parse error"]
fallback ["generic failure"]
]
print loud-parse ; generic failure
; --- retries ---
; Counter closure simulates a flaky operation that fails
; twice then succeeds.
attempts: 0
flaky: does [
attempts: attempts + 1
either attempts < 3 [
error 'network "try again"
] [
"success"
]
]
out: attempt [
source [flaky]
retries 3
catch 'network ["gave up"]
]
print out ; success
print attempts ; 3