-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.test.ts
110 lines (85 loc) · 2.24 KB
/
errors.test.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { asserts } from './deps.ts'
import { bindErrorRecovery, prependErrorMessageWith, retryFailures, withAsyncErrorRecovery, withErrorRecovery } from './errors.ts'
Deno.test('bindErrorRecovery works', () => {
function foo(error: boolean) {
if (error) throw new Error('I throw')
return 12
}
const safeFoo = bindErrorRecovery(foo, 20)
asserts.assertThrows(() => foo(true))
asserts.assertEquals(safeFoo(true), 20)
asserts.assertEquals(safeFoo(false), 12)
})
Deno.test('bindErrorRecovery works on promises', async () => {
async function foo(error: boolean) {
await new Promise((resolve) => setTimeout(resolve, 5))
if (error) throw new Error('I throw')
return 12
}
const safeFoo = bindErrorRecovery(foo, 20)
asserts.assertRejects(async () => await foo(true))
asserts.assertEquals(await safeFoo(true), 20)
asserts.assertEquals(await safeFoo(false), 12)
})
Deno.test('withErrorRecovery works', () => {
asserts.assertEquals(withErrorRecovery(() => 12, 13), 12)
asserts.assertEquals(
withErrorRecovery(() => {
throw new Error('error')
}, 13),
13,
)
})
Deno.test('withAsyncErrorRecovery works', async () => {
asserts.assertEquals(
await withAsyncErrorRecovery(async () => {
await new Promise((resolve) => setTimeout(resolve, 5))
return 12
}, 13),
12,
)
asserts.assertEquals(
await withAsyncErrorRecovery(async () => {
await new Promise((resolve) => setTimeout(resolve, 5))
throw new Error('error')
}, 13),
13,
)
})
Deno.test('retryFailures retries correct number of times', async () => {
let didRun = 0
await retryFailures(
() => {
didRun++
if (didRun < 10) throw new Error('Test error')
},
5,
10,
)
asserts.assertEquals(didRun, 10)
})
Deno.test('retryFailures stops retrying on success', async () => {
let didRun = 0
const result = await retryFailures(
() => {
didRun++
if (didRun < 5) throw new Error('Test error')
return 45
},
5,
10,
)
asserts.assertEquals(result, 45)
asserts.assertEquals(didRun, 5)
})
Deno.test('prependErrorMessageWith prepends error messages with', () => {
asserts.assertThrows(
() => {
const error = new Error('Test error')
prependErrorMessageWith(error, 'prepend stuff')
throw error
},
Error,
'prepend stuff Test error',
)
})