|
1 | | -export class CliError extends Error { |
2 | | - public readonly code: CliError.Code; |
3 | | - public readonly docsLink?: string; |
4 | | - |
5 | | - constructor({ message, code, docsLink }: { message: string; code: CliError.Code; docsLink?: string }) { |
6 | | - super(message); |
7 | | - Object.setPrototypeOf(this, CliError.prototype); |
8 | | - this.code = code; |
9 | | - this.docsLink = docsLink; |
10 | | - } |
| 1 | +export const CliError.Code = { |
| 2 | + InternalError: "INTERNAL_ERROR", |
| 3 | + ResolutionError: "RESOLUTION_ERROR", |
| 4 | + IrConversionError: "IR_CONVERSION_ERROR", |
| 5 | + ContainerError: "CONTAINER_ERROR", |
| 6 | + VersionError: "VERSION_ERROR", |
| 7 | + ParseError: "PARSE_ERROR", |
| 8 | + EnvironmentError: "ENVIRONMENT_ERROR", |
| 9 | + ReferenceError: "REFERENCE_ERROR", |
| 10 | + ValidationError: "VALIDATION_ERROR", |
| 11 | + NetworkError: "NETWORK_ERROR", |
| 12 | + AuthError: "AUTH_ERROR", |
| 13 | + ConfigError: "CONFIG_ERROR", |
| 14 | + Unclassified: "UNCLASSIFIED" |
| 15 | +} as const; |
11 | 16 |
|
12 | | - public static authRequired(message?: string): CliError { |
13 | | - return new CliError({ |
14 | | - message: |
15 | | - message ?? |
16 | | - "Authentication required. Please run 'fern login' or set the FERN_TOKEN environment variable.", |
17 | | - code: CliError.Code.AuthError |
18 | | - }); |
19 | | - } |
20 | | - |
21 | | - public static unauthorized(message?: string): CliError { |
22 | | - return new CliError({ |
23 | | - message: |
24 | | - message ?? "Unauthorized. Please run 'fern auth login' or set the FERN_TOKEN environment variable.", |
25 | | - code: CliError.Code.AuthError |
26 | | - }); |
27 | | - } |
28 | | - |
29 | | - public static notFound(message: string): CliError { |
30 | | - return new CliError({ message, code: CliError.Code.ConfigError }); |
31 | | - } |
32 | | - |
33 | | - public static badRequest(message: string): CliError { |
34 | | - return new CliError({ message, code: CliError.Code.NetworkError }); |
35 | | - } |
36 | | - |
37 | | - public static validationError(message: string): CliError { |
38 | | - return new CliError({ message, code: CliError.Code.ValidationError }); |
39 | | - } |
40 | | - |
41 | | - public static internalError(message: string): CliError { |
42 | | - return new CliError({ message, code: CliError.Code.InternalError }); |
43 | | - } |
44 | | -} |
45 | | - |
46 | | -export namespace CliError { |
47 | | - export type Code = (typeof Code)[keyof typeof Code]; |
48 | | - export const Code = { |
49 | | - InternalError: "INTERNAL_ERROR", |
50 | | - ResolutionError: "RESOLUTION_ERROR", |
51 | | - IrConversionError: "IR_CONVERSION_ERROR", |
52 | | - ContainerError: "CONTAINER_ERROR", |
53 | | - VersionError: "VERSION_ERROR", |
54 | | - ParseError: "PARSE_ERROR", |
55 | | - EnvironmentError: "ENVIRONMENT_ERROR", |
56 | | - ReferenceError: "REFERENCE_ERROR", |
57 | | - ValidationError: "VALIDATION_ERROR", |
58 | | - NetworkError: "NETWORK_ERROR", |
59 | | - AuthError: "AUTH_ERROR", |
60 | | - ConfigError: "CONFIG_ERROR", |
61 | | - Unclassified: "UNCLASSIFIED" |
62 | | - } as const; |
63 | | -} |
| 17 | +export type CliError.Code = (typeof CliError.Code)[keyof typeof CliError.Code]; |
64 | 18 |
|
65 | 19 | const SENTRY_REPORTABLE: Record<CliError.Code, boolean> = { |
66 | 20 | INTERNAL_ERROR: true, |
@@ -106,10 +60,55 @@ export function resolveErrorCode(error: unknown, explicitCode?: CliError.Code): |
106 | 60 | return error.code; |
107 | 61 | } |
108 | 62 | if (isSchemaValidationError(error)) { |
109 | | - return CliError.Code.ParseError; |
| 63 | + return "PARSE_ERROR"; |
110 | 64 | } |
111 | 65 | if (isNodeVersionError(error)) { |
112 | | - return CliError.Code.EnvironmentError; |
| 66 | + return "ENVIRONMENT_ERROR"; |
| 67 | + } |
| 68 | + return "UNCLASSIFIED"; |
| 69 | +} |
| 70 | + |
| 71 | +export class CliError extends Error { |
| 72 | + public readonly code: CliError.Code; |
| 73 | + public readonly docsLink?: string; |
| 74 | + |
| 75 | + constructor({ message, code, docsLink }: { message: string; code: CliError.Code; docsLink?: string }) { |
| 76 | + super(message); |
| 77 | + Object.setPrototypeOf(this, CliError.prototype); |
| 78 | + this.code = code; |
| 79 | + this.docsLink = docsLink; |
| 80 | + } |
| 81 | + |
| 82 | + public static authRequired(message?: string): CliError { |
| 83 | + return new CliError({ |
| 84 | + message: |
| 85 | + message ?? |
| 86 | + "Authentication required. Please run 'fern login' or set the FERN_TOKEN environment variable.", |
| 87 | + code: "AUTH_ERROR" |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + public static unauthorized(message?: string): CliError { |
| 92 | + return new CliError({ |
| 93 | + message: |
| 94 | + message ?? "Unauthorized. Please run 'fern auth login' or set the FERN_TOKEN environment variable.", |
| 95 | + code: "AUTH_ERROR" |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + public static notFound(message: string): CliError { |
| 100 | + return new CliError({ message, code: "CONFIG_ERROR" }); |
| 101 | + } |
| 102 | + |
| 103 | + public static badRequest(message: string): CliError { |
| 104 | + return new CliError({ message, code: "NETWORK_ERROR" }); |
| 105 | + } |
| 106 | + |
| 107 | + public static validationError(message: string): CliError { |
| 108 | + return new CliError({ message, code: "VALIDATION_ERROR" }); |
| 109 | + } |
| 110 | + |
| 111 | + public static internalError(message: string): CliError { |
| 112 | + return new CliError({ message, code: "INTERNAL_ERROR" }); |
113 | 113 | } |
114 | | - return CliError.Code.Unclassified; |
115 | 114 | } |
0 commit comments