-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patherrors.js
More file actions
43 lines (40 loc) · 1.23 KB
/
Copy patherrors.js
File metadata and controls
43 lines (40 loc) · 1.23 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
const TRANSIENT_HTTP_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504]);
const TRANSIENT_ERROR_CODES = new Set([
'ECONNRESET',
'ECONNREFUSED',
'EHOSTUNREACH',
'ENETDOWN',
'ENETRESET',
'ENETUNREACH',
'EPIPE',
'ETIMEDOUT',
'EAI_AGAIN'
]);
export class HttpError extends Error {
constructor(message, details = {}) {
super(message);
this.name = 'HttpError';
this.method = details.method;
this.url = details.url;
this.status = details.status;
this.body = details.body;
this.transient = TRANSIENT_HTTP_STATUSES.has(details.status);
}
}
export function isTransientError(error) {
if (!error) return false;
if (error.transient === true) return true;
if (TRANSIENT_HTTP_STATUSES.has(error.status)) return true;
if (TRANSIENT_ERROR_CODES.has(error.code)) return true;
if (error.name === 'AbortError') return true;
if (error.cause && isTransientError(error.cause)) return true;
return false;
}
export function errorSummary(error) {
if (!error) return 'unknown error';
const parts = [error.name || 'Error'];
if (error.status) parts.push(`status=${error.status}`);
if (error.code) parts.push(`code=${error.code}`);
if (error.message) parts.push(error.message);
return parts.join(' ');
}