-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (58 loc) · 2.12 KB
/
Copy pathindex.js
File metadata and controls
66 lines (58 loc) · 2.12 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
/**
* Error class for non-ok HTTP responses from the Fetch API.
* @extends Error
*/
class HttpError extends Error {
/**
* @param {Response} response - The fetch Response object.
*/
constructor(response) {
super(`${response.status} ${response.statusText}`, { cause: response });
this.name = 'HttpError';
}
/**
* Create an HttpError from a fetch Response, optionally with an already-parsed body.
*
* Pass just the `Response` — e.g. for a non-ok status — and the body is captured via
* `response.clone()`, leaving the original intact. When you've already read the body — e.g.
* a 200 response with an `errors[]` envelope, read with `await response.json()` — pass it as
* the second argument so it isn't re-read (a `Response` body can only be read once). Either
* way the error keeps the response status and `cause`.
*
* @param {Response} response - The fetch Response object.
* @param {object} [json] - An already-parsed JSON body, used instead of reading the response.
* @returns {Promise<HttpError>} Error with text and json properties.
*/
static async from(response, json) {
const err = new HttpError(response);
if (json !== undefined) {
err.json = json;
try {
err.text = JSON.stringify(json);
} catch {
// Body is not serializable
}
} else {
try {
err.text = await response.clone().text();
} catch {
// Body already consumed or otherwise unreadable
}
if (err.text) {
try {
err.json = JSON.parse(err.text);
} catch {
// Response body is not JSON
}
}
}
if (err.json?.errors?.length) {
const messages = err.json.errors.map(e => e.message ?? e.detail).filter(Boolean);
if (messages.length) {
err.message = messages.join('; ');
}
}
return err;
}
}
module.exports = HttpError;