Skip to content

Commit 63429f1

Browse files
authored
Implemented http-fetch's own exception. (#727)
- User can handle HTTP response body and headers. Ref. #723
1 parent 93df966 commit 63429f1

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

lib/exceptions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@ export class HTTPError extends Error {
4242
super(message);
4343
}
4444
}
45+
46+
export class HTTPFetchError extends Error {
47+
constructor(
48+
public statusCode: number,
49+
public statusMessage: string,
50+
public headers: Headers,
51+
public body: string,
52+
) {
53+
super(`${statusCode} - ${statusMessage}`);
54+
}
55+
}

lib/http-fetch.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Readable } from "stream";
2-
import { HTTPError } from "./exceptions";
2+
import { HTTPError, HTTPFetchError } from "./exceptions";
33
import * as qs from "querystring";
44

55
const pkg = require("../package.json");
@@ -35,7 +35,7 @@ export default class HTTPFetchClient {
3535
const response = await fetch(requestUrl, {
3636
headers: this.defaultHeaders,
3737
});
38-
this.checkResponseStatus(response);
38+
await this.checkResponseStatus(response);
3939
return response.json();
4040
}
4141

@@ -75,7 +75,7 @@ export default class HTTPFetchClient {
7575
},
7676
body: JSON.stringify(body),
7777
});
78-
this.checkResponseStatus(response);
78+
await this.checkResponseStatus(response);
7979
return this.responseParse(response);
8080
}
8181

@@ -99,7 +99,7 @@ export default class HTTPFetchClient {
9999
},
100100
body: JSON.stringify(body),
101101
});
102-
this.checkResponseStatus(response);
102+
await this.checkResponseStatus(response);
103103
return this.responseParse(response);
104104
}
105105

@@ -113,7 +113,7 @@ export default class HTTPFetchClient {
113113
},
114114
body: qs.stringify(body),
115115
});
116-
this.checkResponseStatus(response);
116+
await this.checkResponseStatus(response);
117117
return response.json();
118118
}
119119

@@ -126,7 +126,7 @@ export default class HTTPFetchClient {
126126
},
127127
body: form,
128128
});
129-
this.checkResponseStatus(response);
129+
await this.checkResponseStatus(response);
130130
return response.json();
131131
}
132132

@@ -144,7 +144,7 @@ export default class HTTPFetchClient {
144144
},
145145
body: form,
146146
});
147-
this.checkResponseStatus(response);
147+
await this.checkResponseStatus(response);
148148
return response.json();
149149
}
150150
public async postBinaryContent<T>(url: string, body: Blob): Promise<T> {
@@ -157,7 +157,7 @@ export default class HTTPFetchClient {
157157
},
158158
body: body,
159159
});
160-
this.checkResponseStatus(response);
160+
await this.checkResponseStatus(response);
161161
return response.json();
162162
}
163163

@@ -172,17 +172,17 @@ export default class HTTPFetchClient {
172172
...this.defaultHeaders,
173173
},
174174
});
175-
this.checkResponseStatus(response);
175+
await this.checkResponseStatus(response);
176176
return response.json();
177177
}
178178

179-
private checkResponseStatus(response: Response) {
179+
private async checkResponseStatus(response: Response) {
180180
if (!response.ok) {
181-
throw new HTTPError(
182-
"HTTP request failed",
181+
throw new HTTPFetchError(
183182
response.status,
184183
response.statusText,
185-
undefined,
184+
response.headers,
185+
await response.text()
186186
);
187187
}
188188
}

test/http-fetch.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { deepEqual, equal, ok } from "assert";
2-
import { HTTPError } from "../lib";
2+
import { HTTPError, HTTPFetchError } from "../lib";
33
import HTTPFetchClient from "../lib/http-fetch";
44
import { getStreamData } from "./helpers/stream";
55
import { http, HttpResponse } from "msw";
@@ -211,17 +211,19 @@ describe("http(fetch)", () => {
211211
http.get(baseURL + "/404", async ({ request, params, cookies }) => {
212212
scope.done();
213213
equal(request.headers.get("user-agent"), `${pkg.name}/${pkg.version}`);
214-
return HttpResponse.json(404, { status: 404 });
214+
return HttpResponse.json({reason: "not found"}, { status: 404 });
215215
}),
216216
);
217217

218218
try {
219219
await client.get(`/404`);
220220
ok(false);
221221
} catch (err) {
222-
ok(err instanceof HTTPError);
222+
ok(err instanceof HTTPFetchError);
223223
equal(scope.isDone(), true);
224224
equal(err.statusCode, 404);
225+
equal(err.headers.get('content-type'), "application/json");
226+
equal(err.body, "{\"reason\":\"not found\"}");
225227
}
226228
});
227229

0 commit comments

Comments
 (0)