Repository navigation
Expand file tree
/
Copy patherrors.ts
More file actions
186 lines (161 loc) · 5.63 KB
/
Copy patherrors.ts
File metadata and controls
186 lines (161 loc) · 5.63 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* Typed HTTP error hierarchy for @eserstack/httpclient.
*
* Replaces the duplicated status-code classification logic in:
* - @eserstack/ai/errors.ts (classifyStatusCode / classifyAndWrap)
* - @eserstack/posts adapters (extractError helpers)
*
* After migration, those packages import from here instead.
*
* @module
*/
// =============================================================================
// Error Options
// =============================================================================
export interface HttpClientErrorOptions {
statusCode?: number;
request?: Request;
response?: Response;
body?: unknown;
retryAfter?: number;
cause?: Error;
}
// =============================================================================
// Error Hierarchy
// =============================================================================
export class HttpClientError extends Error {
readonly statusCode: number | null;
readonly request: Request | null;
readonly response: Response | null;
readonly body: unknown;
readonly retryAfter: number | null;
constructor(message: string, options?: HttpClientErrorOptions) {
super(message, { cause: options?.cause });
this.name = "HttpClientError";
this.statusCode = options?.statusCode ?? null;
this.request = options?.request ?? null;
this.response = options?.response ?? null;
this.body = options?.body;
this.retryAfter = options?.retryAfter ?? null;
}
}
/** Non-2xx HTTP response (generic). */
export class HttpResponseError extends HttpClientError {
readonly code = "RESPONSE_ERROR" as const;
constructor(message: string, options?: HttpClientErrorOptions) {
super(message, options);
this.name = "HttpResponseError";
}
}
/** Request timed out via AbortSignal.timeout(). */
export class HttpTimeoutError extends HttpClientError {
readonly code = "TIMEOUT" as const;
constructor(message: string, options?: HttpClientErrorOptions) {
super(message, options);
this.name = "HttpTimeoutError";
}
}
/** Request aborted via a user-supplied AbortSignal. */
export class HttpAbortError extends HttpClientError {
readonly code = "ABORT_ERROR" as const;
constructor(message: string, options?: HttpClientErrorOptions) {
super(message, options);
this.name = "HttpAbortError";
}
}
/** HTTP 429 Too Many Requests. */
export class HttpRateLimitError extends HttpClientError {
readonly code = "RATE_LIMITED" as const;
constructor(message: string, options?: HttpClientErrorOptions) {
super(message, options);
this.name = "HttpRateLimitError";
}
}
/** DNS lookup or TCP connection failure (no HTTP response received). */
export class HttpNetworkError extends HttpClientError {
readonly code = "NETWORK_ERROR" as const;
constructor(message: string, options?: HttpClientErrorOptions) {
super(message, options);
this.name = "HttpNetworkError";
}
}
// =============================================================================
// Classification Utilities
// =============================================================================
/**
* Maps an HTTP status code to the appropriate error class.
* Returns null for status codes that don't have a dedicated type.
*
* Replaces @eserstack/ai/errors.ts:classifyStatusCode with HTTP-generic semantics
* (no AI-specific codes, no provider field).
*/
export const classifyHttpStatus = (
statusCode: number,
): typeof HttpClientError | null => {
if (statusCode === 429) return HttpRateLimitError;
if (statusCode >= 500 && statusCode <= 599) return HttpResponseError;
if (statusCode === 408) return HttpResponseError;
return null;
};
/**
* Converts a native Error (or existing HttpClientError) to the most specific
* HttpClientError subclass. Inspects error.name for timeout/abort signals.
*/
export const classifyError = (
error: Error,
response?: Response,
): HttpClientError => {
if (error instanceof HttpClientError) return error;
// DOMException: TimeoutError from AbortSignal.timeout()
if (error.name === "TimeoutError") {
return new HttpTimeoutError(error.message, {
cause: error,
...(response !== undefined && {
statusCode: response.status,
response,
}),
});
}
// DOMException: AbortError from user-supplied AbortSignal
if (error.name === "AbortError") {
return new HttpAbortError(error.message, { cause: error });
}
if (response !== undefined) {
const ErrorClass = classifyHttpStatus(response.status);
const opts: HttpClientErrorOptions = {
statusCode: response.status,
response,
cause: error,
};
return ErrorClass !== null
? new ErrorClass(error.message, opts)
: new HttpResponseError(error.message, opts);
}
// Network-level failures (no response)
if (
error.message.includes("Failed to fetch") ||
error.message.includes("fetch failed") ||
error.message.includes("ECONNREFUSED") ||
error.message.includes("ENOTFOUND")
) {
return new HttpNetworkError(error.message, { cause: error });
}
return new HttpClientError(error.message, { cause: error });
};
/**
* Wrap a native Error with a classified HttpClientError based on status code.
* Replaces @eserstack/ai/errors.ts:classifyAndWrap — no provider field here.
*/
export const classifyAndWrap = (
statusCode: number,
original: Error,
options?: Omit<HttpClientErrorOptions, "statusCode" | "cause">,
): HttpClientError => {
const ErrorClass = classifyHttpStatus(statusCode) ?? HttpResponseError;
return new ErrorClass(original.message, {
...options,
statusCode,
cause: original,
});
};