Skip to content

Commit

Permalink
Implemented http-fetch's own exception.
Browse files Browse the repository at this point in the history
- User can handle HTTP response body and headers.
  • Loading branch information
tokuhirom committed Mar 1, 2024
1 parent 93df966 commit 3e87eea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
11 changes: 11 additions & 0 deletions lib/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ export class HTTPError extends Error {
super(message);
}
}

export class HTTPFetchError extends Error {
constructor(
public statusCode: number,
public statusMessage: string,
public headers: Headers,
public body: string,
) {
super(`${statusCode} - ${statusMessage}`);
}
}
26 changes: 13 additions & 13 deletions lib/http-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Readable } from "stream";
import { HTTPError } from "./exceptions";
import { HTTPError, HTTPFetchError } from "./exceptions";
import * as qs from "querystring";

const pkg = require("../package.json");
Expand Down Expand Up @@ -35,7 +35,7 @@ export default class HTTPFetchClient {
const response = await fetch(requestUrl, {
headers: this.defaultHeaders,
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return response.json();
}

Expand Down Expand Up @@ -75,7 +75,7 @@ export default class HTTPFetchClient {
},
body: JSON.stringify(body),
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return this.responseParse(response);
}

Expand All @@ -99,7 +99,7 @@ export default class HTTPFetchClient {
},
body: JSON.stringify(body),
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return this.responseParse(response);
}

Expand All @@ -113,7 +113,7 @@ export default class HTTPFetchClient {
},
body: qs.stringify(body),
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return response.json();
}

Expand All @@ -126,7 +126,7 @@ export default class HTTPFetchClient {
},
body: form,
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return response.json();
}

Expand All @@ -144,7 +144,7 @@ export default class HTTPFetchClient {
},
body: form,
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return response.json();
}
public async postBinaryContent<T>(url: string, body: Blob): Promise<T> {
Expand All @@ -157,7 +157,7 @@ export default class HTTPFetchClient {
},
body: body,
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return response.json();
}

Expand All @@ -172,17 +172,17 @@ export default class HTTPFetchClient {
...this.defaultHeaders,
},
});
this.checkResponseStatus(response);
await this.checkResponseStatus(response);
return response.json();
}

private checkResponseStatus(response: Response) {
private async checkResponseStatus(response: Response) {
if (!response.ok) {
throw new HTTPError(
"HTTP request failed",
throw new HTTPFetchError(
response.status,
response.statusText,
undefined,
response.headers,
await response.text()
);
}
}
Expand Down
8 changes: 5 additions & 3 deletions test/http-fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepEqual, equal, ok } from "assert";
import { HTTPError } from "../lib";
import { HTTPError, HTTPFetchError } from "../lib";
import HTTPFetchClient from "../lib/http-fetch";
import { getStreamData } from "./helpers/stream";
import { http, HttpResponse } from "msw";
Expand Down Expand Up @@ -211,17 +211,19 @@ describe("http(fetch)", () => {
http.get(baseURL + "/404", async ({ request, params, cookies }) => {
scope.done();
equal(request.headers.get("user-agent"), `${pkg.name}/${pkg.version}`);
return HttpResponse.json(404, { status: 404 });
return HttpResponse.json({reason: "not found"}, { status: 404 });
}),
);

try {
await client.get(`/404`);
ok(false);
} catch (err) {
ok(err instanceof HTTPError);
ok(err instanceof HTTPFetchError);
equal(scope.isDone(), true);
equal(err.statusCode, 404);
equal(err.headers.get('content-type'), "application/json");
equal(err.body, "{\"reason\":\"not found\"}");
}
});

Expand Down

0 comments on commit 3e87eea

Please sign in to comment.