1 | /**
|
2 | * HttpError extends the Error object, and is thrown wheenever servers emit
|
3 | * HTTP errors.
|
4 | *
|
5 | * It has a response property, allowing users to find out more about the
|
6 | * nature of the error.
|
7 | *
|
8 | * @constructor
|
9 | * @param {Response} response
|
10 | */
|
11 | export class HttpError extends Error {
|
12 |
|
13 | response: Response;
|
14 | status: number;
|
15 |
|
16 | constructor(response: Response) {
|
17 | super('HTTP error ' + response.status);
|
18 | this.response = response;
|
19 | this.status = response.status;
|
20 | }
|
21 |
|
22 | }
|
23 |
|
24 | /**
|
25 | * Problem extends the HttpError object. If a server emits a HTTP error, and
|
26 | * the response body's content-type is application/problem+json.
|
27 | *
|
28 | * application/problem+json is defined in RFC7807 and provides a standardized
|
29 | * way to describe error conditions by a HTTP server.
|
30 | *
|
31 | * @constructor
|
32 | * @param {Response} response
|
33 | * @param {object} problemBody
|
34 | */
|
35 | export class Problem extends HttpError {
|
36 |
|
37 | body: {
|
38 | title?: string
|
39 | };
|
40 |
|
41 | constructor(response: Response, problemBody: object) {
|
42 | super(response);
|
43 | this.body = problemBody;
|
44 | if (this.body.title) {
|
45 | this.message = 'HTTP Error ' + this.status + ': ' + this.body.title;
|
46 | }
|
47 | }
|
48 |
|
49 | }
|
50 |
|
51 | /**
|
52 | * This function creates problems, not unlike the the author of this file.
|
53 | *
|
54 | * It takes a Fetch Response object, and returns a HttpError. If the HTTP
|
55 | * response has a type of application/problem+json it will return a Problem
|
56 | * object.
|
57 | *
|
58 | * Because parsing the response might be asynchronous, the function returns
|
59 | * a Promise resolving in either object.
|
60 | */
|
61 | export default async function problemFactory(response: Response): Promise<HttpError | Problem> {
|
62 |
|
63 | const contentType = response.headers.get('Content-Type');
|
64 | if (contentType && contentType.match(/^application\/problem\+json/i)) {
|
65 | const problemBody = await response.json();
|
66 | return new Problem(response, problemBody);
|
67 | } else {
|
68 | return new HttpError(response);
|
69 | }
|
70 |
|
71 | }
|
72 |
|