Skip to content
Source
Sign inSign up
Open History to see the last commit

error.ts

67 lines1.4 KB

1import type { SystemError, SystemErrorTag } from "effect/PlatformError"
2import * as PlatformError from "effect/PlatformError"
3​
4interface DenoError {
5 readonly code?: unknown
6 readonly name?: unknown
7}
8​
9export const handleError = (
10 module: SystemError["module"],
11 method: string,
12 pathOrDescriptor?: string | number
13) =>
14(error: unknown): PlatformError.PlatformError => {
15 const denoError = error as DenoError
16 let tag: SystemErrorTag = "Unknown"
17​
18 switch (denoError?.name) {
19 case "AlreadyExists":
20 tag = "AlreadyExists"
21 break
22 case "NotCapable":
23 tag = "PermissionDenied"
24 break
25 case "BadResource":
26 case "InvalidData":
27 case "TimedOut":
28 case "UnexpectedEof":
29 case "WouldBlock":
30 case "WriteZero":
31 tag = denoError.name
32 break
33 default:
34 switch (denoError?.code) {
35 case "ENOENT":
36 tag = "NotFound"
37 break
38​
39 case "EACCES":
40 case "EPERM":
41 tag = "PermissionDenied"
42 break
43​
44 case "EEXIST":
45 tag = "AlreadyExists"
46 break
47​
48 case "EISDIR":
49 case "ENOTDIR":
50 case "ELOOP":
51 tag = "BadResource"
52 break
53​
54 case "EBUSY":
55 tag = "Busy"
56 break
57 }
58 }
59​
60 return PlatformError.systemError({
61 _tag: tag,
62 module,
63 method,
64 pathOrDescriptor,
65 cause: error
66 })