| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import { SystemError, type SystemErrorTag } from "effect/PlatformError" |
| 3 | import { handleError } from "../../src/internal/error.ts" |
| 4 | |
| 5 | const withCode = (error: Error, code: string): Error & { readonly code: string } => Object.assign(error, { code }) |
| 6 | |
| 7 | describe("handleError", () => { |
| 8 | const mapError = handleError("FileSystem", "test", "/tmp/test") |
| 9 | const cases: ReadonlyArray<readonly [error: Error, tag: SystemErrorTag]> = [ |
| 10 | [withCode(new Deno.errors.NotFound(), "ENOENT"), "NotFound"], |
| 11 | [withCode(new Deno.errors.NotADirectory(), "ENOTDIR"), "BadResource"], |
| 12 | [new Deno.errors.AlreadyExists(), "AlreadyExists"], |
| 13 | [withCode(new Deno.errors.AlreadyExists(), "EEXIST"), "AlreadyExists"], |
| 14 | [withCode(new Deno.errors.IsADirectory(), "EISDIR"), "BadResource"], |
| 15 | [withCode(new Deno.errors.PermissionDenied(), "EACCES"), "PermissionDenied"], |
| 16 | [new Deno.errors.NotCapable(), "PermissionDenied"], |
| 17 | [new Deno.errors.BadResource(), "BadResource"], |
| 18 | [new Deno.errors.InvalidData(), "InvalidData"], |
| 19 | [new Deno.errors.TimedOut(), "TimedOut"], |
| 20 | [new Deno.errors.UnexpectedEof(), "UnexpectedEof"], |
| 21 | [new Deno.errors.WouldBlock(), "WouldBlock"], |
| 22 | [new Deno.errors.WriteZero(), "WriteZero"], |
| 23 | [new Error("unrecognised"), "Unknown"] |
| 24 | ] |
| 25 | |
| 26 | for (const [error, tag] of cases) { |
| 27 | it(`maps ${error.name} to ${tag}`, () => { |
| 28 | const platformError = mapError(error) |
| 29 | const reason = platformError.reason |
| 30 | |
| 31 | assert(reason instanceof SystemError) |
| 32 | assert.strictEqual(reason._tag, tag) |
| 33 | assert.strictEqual(reason.module, "FileSystem") |
| 34 | assert.strictEqual(reason.method, "test") |
| 35 | assert.strictEqual(reason.pathOrDescriptor, "/tmp/test") |
| 36 | assert.strictEqual(reason.cause, error) |
| 37 | }) |
| 38 | } |
| 39 | }) |