|
1 | 1 | import { describe, expect, it } from "bun:test" |
2 | 2 | import { Panic, UnhandledException, run } from "../index" |
3 | 3 |
|
| 4 | +class InvalidInputError extends Error {} |
| 5 | +class PermissionDeniedError extends Error {} |
| 6 | +class NetworkError extends Error {} |
| 7 | +class RemoteServiceError extends Error {} |
| 8 | + |
4 | 9 | describe("run sync", () => { |
5 | 10 | it("returns value when tryFn succeeds", () => { |
6 | 11 | const value = run(() => 42) |
@@ -39,4 +44,121 @@ describe("run sync", () => { |
39 | 44 | }) |
40 | 45 | ).toThrow(Panic) |
41 | 46 | }) |
| 47 | + |
| 48 | + it("supports multiple mapped error variants in sync object form", () => { |
| 49 | + const invalidInput = run({ |
| 50 | + catch: (error) => { |
| 51 | + if (error instanceof SyntaxError) { |
| 52 | + return new InvalidInputError("invalid") |
| 53 | + } |
| 54 | + |
| 55 | + return new PermissionDeniedError("denied") |
| 56 | + }, |
| 57 | + try: () => { |
| 58 | + throw new SyntaxError("bad input") |
| 59 | + }, |
| 60 | + }) |
| 61 | + |
| 62 | + const permissionDenied = run({ |
| 63 | + catch: (error) => { |
| 64 | + if (error instanceof SyntaxError) { |
| 65 | + return new InvalidInputError("invalid") |
| 66 | + } |
| 67 | + |
| 68 | + return new PermissionDeniedError("denied") |
| 69 | + }, |
| 70 | + try: () => { |
| 71 | + throw new Error("no access") |
| 72 | + }, |
| 73 | + }) |
| 74 | + |
| 75 | + expect(invalidInput).toBeInstanceOf(InvalidInputError) |
| 76 | + expect(permissionDenied).toBeInstanceOf(PermissionDeniedError) |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +describe("run async", () => { |
| 81 | + it("returns promise value when tryFn is async", async () => { |
| 82 | + const result = run(async () => { |
| 83 | + await Promise.resolve() |
| 84 | + |
| 85 | + return 42 |
| 86 | + }) |
| 87 | + |
| 88 | + expect(await result).toBe(42) |
| 89 | + }) |
| 90 | + |
| 91 | + it("returns UnhandledException when async function form rejects", async () => { |
| 92 | + const result = run(async () => { |
| 93 | + await Promise.resolve() |
| 94 | + throw new Error("boom") |
| 95 | + }) |
| 96 | + |
| 97 | + expect(await result).toBeInstanceOf(UnhandledException) |
| 98 | + }) |
| 99 | + |
| 100 | + it("maps async object form rejections through catch", async () => { |
| 101 | + const result = run({ |
| 102 | + catch: () => "mapped", |
| 103 | + try: async () => { |
| 104 | + await Promise.resolve() |
| 105 | + throw new Error("boom") |
| 106 | + }, |
| 107 | + }) |
| 108 | + |
| 109 | + expect(await result).toBe("mapped") |
| 110 | + }) |
| 111 | + |
| 112 | + it("throws Panic when async catch rejects", async () => { |
| 113 | + const result = run({ |
| 114 | + catch: async () => { |
| 115 | + await Promise.resolve() |
| 116 | + throw new Error("catch failed") |
| 117 | + }, |
| 118 | + try: async () => { |
| 119 | + await Promise.resolve() |
| 120 | + throw new Error("boom") |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + try { |
| 125 | + await result |
| 126 | + throw new Error("Expected Panic rejection") |
| 127 | + } catch (error) { |
| 128 | + expect(error).toBeInstanceOf(Panic) |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + it("supports multiple mapped error variants in async object form", async () => { |
| 133 | + const networkError = await run({ |
| 134 | + catch: (error): NetworkError | RemoteServiceError => { |
| 135 | + if (error instanceof TypeError) { |
| 136 | + return new NetworkError("network") |
| 137 | + } |
| 138 | + |
| 139 | + return new RemoteServiceError("remote") |
| 140 | + }, |
| 141 | + try: async () => { |
| 142 | + await Promise.resolve() |
| 143 | + throw new TypeError("fetch failed") |
| 144 | + }, |
| 145 | + }) |
| 146 | + |
| 147 | + const remoteServiceError = await run({ |
| 148 | + catch: (error) => { |
| 149 | + if (error instanceof TypeError) { |
| 150 | + return new NetworkError("network") |
| 151 | + } |
| 152 | + |
| 153 | + return new RemoteServiceError("remote") |
| 154 | + }, |
| 155 | + try: async () => { |
| 156 | + await Promise.resolve() |
| 157 | + throw new Error("500") |
| 158 | + }, |
| 159 | + }) |
| 160 | + |
| 161 | + expect(networkError).toBeInstanceOf(NetworkError) |
| 162 | + expect(remoteServiceError).toBeInstanceOf(RemoteServiceError) |
| 163 | + }) |
42 | 164 | }) |
0 commit comments