-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
80 lines (75 loc) · 2.32 KB
/
errors.ts
File metadata and controls
80 lines (75 loc) · 2.32 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
export enum ErrorType {
Unknown = 'unknown',
MissingAuthToken = 'missing_token',
AuthTokenInvalid = 'invalid_token',
AuthTokenWrongSignature = 'wrong_signature',
AuthTokenExpired = 'expired_token',
AccessDenied = 'access_denied',
NotFound = 'not_found',
ServerMisconfigured = 'server_misconfigured',
InvalidRequest = 'invalid_request',
CapabilityNotAvailable = 'capability_not_available',
}
type StatusCode = 200 | 201 | 204 | 400 | 401 | 403 | 404 | 500;
type ExtensionErrorOptions = {
message?: string; // User-friendly message describing what happened
responsePayload?: Record<string, unknown>; // Extra JSON data to be returned to the client
debugPayload?: Record<string, unknown>; // Extra JSON data for debugging. Not returned to the client in production
cause?: unknown; // The underlying error, if any
};
export type ErrorPayload = {
type: ErrorType;
message: string;
data: Record<string, unknown>;
debug?: {
stack?: string;
underlying?: {
message: string;
stack?: string;
};
} & Record<string, unknown>;
};
export class RuntError extends Error {
constructor(
public type: ErrorType,
private options: ExtensionErrorOptions = {}
) {
super(options.message ?? `RuntError: ${type}`, { cause: options.cause });
}
get statusCode(): StatusCode {
const StatusCodeMapping: Record<ErrorType, StatusCode> = {
[ErrorType.InvalidRequest]: 400,
[ErrorType.CapabilityNotAvailable]: 400,
[ErrorType.MissingAuthToken]: 401,
[ErrorType.AuthTokenInvalid]: 401,
[ErrorType.AuthTokenExpired]: 401,
[ErrorType.AuthTokenWrongSignature]: 401,
[ErrorType.AccessDenied]: 403,
[ErrorType.NotFound]: 404,
[ErrorType.ServerMisconfigured]: 500,
[ErrorType.Unknown]: 500,
};
return StatusCodeMapping[this.type];
}
public getPayload(debug: boolean): ErrorPayload {
const underlying =
this.cause instanceof Error
? {
message: this.cause.message,
stack: this.cause.stack,
}
: undefined;
return {
type: this.type,
message: this.message,
data: this.options.responsePayload ?? {},
debug: debug
? {
stack: this.stack,
underlying,
...this.options.debugPayload,
}
: undefined,
};
}
}