-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Vehmloewff/error-recovery-utils
feat(errors): bindErrorRecovery, withErrorRecovery, withAsyncErrorRecovery
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { asserts } from './deps.ts' | ||
import { bindErrorRecovery, 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, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters