-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handler.ts
More file actions
120 lines (110 loc) · 2.81 KB
/
Copy patherror-handler.ts
File metadata and controls
120 lines (110 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Top-level error handler for the CLI.
*
* Maps custom error types to structured JSON output and exit codes.
* Exit codes: 1=validation, 2=git, 3=filesystem, 4=business logic.
*
* Note: Error classes are imported from schema/errors.ts (created in Plan 02).
* This file sets up the handler structure first.
*/
import type {
ValidationError,
GitError,
FsError,
BusinessError,
} from '../schema/errors.js';
/**
* Handles top-level errors with proper exit codes.
*
* Uses instanceof type narrowing to map error types to exit codes:
* - ValidationError: exit code 1
* - GitError: exit code 2
* - FsError: exit code 3
* - BusinessError: exit code 4
* - Unknown errors: exit code 1
*
* @param err - The error to handle
* @returns Never (always exits process)
*/
export function handleTopLevelError(err: unknown): never {
// ValidationError: exit code 1
if (isValidationError(err)) {
console.error(JSON.stringify(err.toJSON()));
process.exit(1);
}
// GitError: exit code 2
if (isGitError(err)) {
console.error(JSON.stringify(err.toJSON()));
process.exit(2);
}
// FsError: exit code 3
if (isFsError(err)) {
console.error(JSON.stringify(err.toJSON()));
process.exit(3);
}
// BusinessError: exit code 4
if (isBusinessError(err)) {
console.error(JSON.stringify(err.toJSON()));
process.exit(4);
}
// Unknown error: exit code 1
const unknownErr = {
error: 'UNKNOWN_ERROR',
message: err instanceof Error ? err.message : 'Unknown error',
};
console.error(JSON.stringify(unknownErr));
process.exit(1);
}
/**
* Type guard for ValidationError.
* Works with duck typing since class is defined in Plan 02.
*/
function isValidationError(err: unknown): err is ValidationError {
return (
err instanceof Error &&
'exitCode' in err &&
(err as ValidationError).exitCode === 1 &&
'code' in err &&
(err as ValidationError).code === 'VALIDATION_ERROR' &&
'toJSON' in err
);
}
/**
* Type guard for GitError.
*/
function isGitError(err: unknown): err is GitError {
return (
err instanceof Error &&
'exitCode' in err &&
(err as GitError).exitCode === 2 &&
'code' in err &&
(err as GitError).code === 'GIT_ERROR' &&
'toJSON' in err
);
}
/**
* Type guard for FsError.
*/
function isFsError(err: unknown): err is FsError {
return (
err instanceof Error &&
'exitCode' in err &&
(err as FsError).exitCode === 3 &&
'code' in err &&
(err as FsError).code === 'FS_ERROR' &&
'toJSON' in err
);
}
/**
* Type guard for BusinessError.
*/
function isBusinessError(err: unknown): err is BusinessError {
return (
err instanceof Error &&
'exitCode' in err &&
(err as BusinessError).exitCode === 4 &&
'code' in err &&
(err as BusinessError).code === 'BUSINESS_ERROR' &&
'toJSON' in err
);
}