1import type { SystemError, SystemErrorTag } from "effect/PlatformError"
2import * as PlatformError from "effect/PlatformError"
3import type { PathLike } from "node:fs"
4
5/** @internal */
6export const handleErrnoException = (module: SystemError["module"], method: string) =>
7(
8 err: NodeJS.ErrnoException,
9 [path]: [path: PathLike | number | string | ReadonlyArray<string>, ...args: Array<any>]
10): PlatformError.PlatformError => {
11 let reason: SystemErrorTag = "Unknown"
12
13 switch (err.code) {
14 case "ENOENT":
15 reason = "NotFound"
16 break
17
18 case "EACCES":
19 reason = "PermissionDenied"
20 break
21
22 case "EEXIST":
23 reason = "AlreadyExists"
24 break
25
26 case "EISDIR":
27 reason = "BadResource"
28 break
29
30 case "ENOTDIR":
31 reason = "BadResource"
32 break
33
34 case "EBUSY":
35 reason = "Busy"
36 break
37
38 case "ELOOP":
39 reason = "BadResource"
40 break
41 }
42
43 return PlatformError.systemError({
44 _tag: reason,
45 module,
46 method,
47 pathOrDescriptor: path as string | number,
48 syscall: err.syscall,
49 cause: err
50 })
51}