-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy patherrors.ts
More file actions
35 lines (33 loc) · 1.06 KB
/
errors.ts
File metadata and controls
35 lines (33 loc) · 1.06 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
type SwcError = {
code: "UnsupportedSyntax" | "InvalidSyntax";
message: string;
startColumn: number;
startLine: number;
snippet: string;
filename: string;
endColumn: number;
endLine: number;
};
// Type guard to check if error is SwcError
export function isSwcError(error: unknown): error is SwcError {
return (error as SwcError).code !== undefined;
}
// Since swc throw an object, we need to wrap it in a proper error
export function wrapAndReThrowSwcError(error: SwcError): never {
const errorHints = `${error.filename}:${error.startLine}\n${error.snippet}\n`;
switch (error.code) {
case "UnsupportedSyntax": {
const unsupportedSyntaxError = new Error(error.message);
unsupportedSyntaxError.name = "UnsupportedSyntaxError";
unsupportedSyntaxError.stack = `${errorHints}${unsupportedSyntaxError.stack}`;
throw unsupportedSyntaxError;
}
case "InvalidSyntax": {
const syntaxError = new SyntaxError(error.message);
syntaxError.stack = `${errorHints}${syntaxError.stack}`;
throw syntaxError;
}
default:
throw new Error(error.message);
}
}