-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy patherrors.ts
More file actions
75 lines (69 loc) · 1.7 KB
/
Copy patherrors.ts
File metadata and controls
75 lines (69 loc) · 1.7 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
export type ApiErrorCode =
| 'unauthorized'
| 'forbidden'
| 'not_found'
| 'validation_error'
| 'rate_limited'
| 'network_error'
| 'server_error'
| 'service_unavailable'
| 'bad_request'
| 'unknown_error'
| 'aborted';
export interface ApiErrorOptions {
status?: number;
code: ApiErrorCode;
safeMessage: string;
path?: string;
retryable?: boolean;
details?: Record<string, unknown>;
cause?: unknown;
}
export class ApiError extends Error {
readonly status?: number;
readonly code: ApiErrorCode;
readonly safeMessage: string;
readonly path?: string;
readonly retryable: boolean;
readonly details?: Record<string, unknown>;
constructor({
status,
code,
safeMessage,
path,
retryable = false,
details,
cause,
}: ApiErrorOptions) {
super(safeMessage);
this.name = 'ApiError';
this.status = status;
this.code = code;
this.safeMessage = safeMessage;
this.path = path;
this.retryable = retryable;
this.details = details;
if (cause !== undefined) {
;(this as Error & { cause?: unknown }).cause = cause;
}
}
}
/**
* Represents an offline or degraded‑mode error. It is a specific kind of
* `ApiError` with the code `service_unavailable` and is not retryable. UI
* components can catch this type to display an offline banner.
*/
export class OfflineError extends ApiError {
constructor(message = 'The application is offline. Showing cached data.') {
super({
status: 503,
code: 'service_unavailable',
safeMessage: message,
retryable: false,
});
this.name = 'OfflineError';
}
}
export function isApiError(err: unknown): err is ApiError {
return err instanceof ApiError;
}