forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk-error.ts
More file actions
142 lines (119 loc) · 4.07 KB
/
Copy pathsdk-error.ts
File metadata and controls
142 lines (119 loc) · 4.07 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
export interface LilyRequestMetadata {
method: string;
path: string;
url: string;
}
export interface LilyErrorOptions {
code?: string;
statusCode?: number;
details?: unknown;
cause?: unknown;
request?: LilyRequestMetadata;
/** Redacted excerpt of the response body, safe to log or send to a bug tracker. */
bodySnippet?: string;
/** Delta-seconds value from a Retry-After header, when present. */
retryAfterSeconds?: number;
/** Response headers from the failing HTTP response. */
headers?: Record<string, string>;
}
export const LILY_ERROR_CODES = Object.freeze({
CONFIG_ERROR: 'CONFIG_ERROR',
API_ERROR: 'API_ERROR',
AUTHENTICATION_ERROR: 'AUTHENTICATION_ERROR',
AUTHORIZATION_ERROR: 'AUTHORIZATION_ERROR',
VALIDATION_ERROR: 'VALIDATION_ERROR',
RESPONSE_VALIDATION_ERROR: 'RESPONSE_VALIDATION_ERROR',
TRANSPORT_ERROR: 'TRANSPORT_ERROR',
NOT_FOUND: 'NOT_FOUND',
CONFLICT: 'CONFLICT',
RATE_LIMITED: 'RATE_LIMITED',
SERVER_ERROR: 'SERVER_ERROR',
TIMEOUT: 'TIMEOUT',
CANCELLED: 'CANCELLED',
RESPONSE_VALIDATION_ERROR: 'RESPONSE_VALIDATION_ERROR',
});
export type LilyErrorCode = keyof typeof LILY_ERROR_CODES;
export class LilySdkError extends Error {
public readonly code: string | undefined;
public readonly statusCode: number | undefined;
public readonly details: unknown;
public readonly request: LilyRequestMetadata | undefined;
public readonly headers: Record<string, string> | undefined;
public constructor(message: string, options: LilyErrorOptions = {}) {
super(message, { cause: options.cause });
this.name = new.target.name;
this.code = options.code;
this.statusCode = options.statusCode;
this.details = options.details;
this.request = options.request;
this.headers = options.headers;
}
public toJSON(): Record<string, unknown> {
const result: Record<string, unknown> = {
name: this.name,
message: this.message,
};
if (this.code !== undefined) {
result.code = this.code;
}
if (this.statusCode !== undefined) {
result.statusCode = this.statusCode;
}
if (this.details !== undefined) {
result.details = this.details;
}
if (this.request !== undefined) {
result.request = this.request;
}
if (this.headers !== undefined) {
result.headers = this.headers;
}
if (this.headers !== undefined) {
result.headers = this.headers;
}
const cause = this.cause;
if (cause !== undefined && cause !== null) {
result.cause =
cause instanceof LilySdkError
? cause.toJSON()
: cause instanceof Error
? { name: cause.name, message: cause.message }
: cause;
}
return result;
}
public override toString(): string {
const parts: string[] = [this.name, this.message];
if (this.code !== undefined) {
parts.push(`[${this.code}]`);
}
if (this.statusCode !== undefined) {
parts.push(`(HTTP ${this.statusCode})`);
}
return parts.join(': ');
}
}
export class LilyConfigError extends LilySdkError {}
export class LilyTransportError extends LilySdkError {}
export class LilyAuthenticationError extends LilySdkError {}
/**
* Any non-ok HTTP response. The subclasses below narrow it, so
* `catch (e) { if (e instanceof LilyApiError) ... }` keeps working for callers
* that only care that the API rejected the request.
*/
export class LilyApiError extends LilySdkError {}
export class LilyValidationError extends LilySdkError {}
export class LilyAuthorizationError extends LilyAuthenticationError {}
export class LilyNotFoundError extends LilyApiError {}
export class LilyConflictError extends LilyApiError {}
export class LilyServerError extends LilyApiError {}
export class LilyRateLimitError extends LilyApiError {
public readonly retryAfterSeconds: number | undefined;
public constructor(message: string, options: LilyErrorOptions = {}) {
super(message, options);
this.retryAfterSeconds = options.retryAfterSeconds;
}
}
export function isLilySdkError(value: unknown): value is LilySdkError {
return value instanceof LilySdkError;
}