Skip to content

Commit c59b35a

Browse files
committed
Handle http errors
1 parent e3197cc commit c59b35a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/lib/graphql.ts

+19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ export async function executeGraphQL<Result, Variables>(
3838
next: { revalidate },
3939
});
4040

41+
if (!response.ok) {
42+
const body = await (async () => {
43+
try {
44+
return await response.text();
45+
} catch {
46+
return "";
47+
}
48+
})();
49+
throw new HTTPError(response, body);
50+
}
51+
4152
const body = (await response.json()) as GraphQLRespone<Result>;
4253

4354
if ("errors" in body) {
@@ -55,6 +66,14 @@ export class GraphQLError extends Error {
5566
Object.setPrototypeOf(this, new.target.prototype);
5667
}
5768
}
69+
export class HTTPError extends Error {
70+
constructor(response: Response, body: string) {
71+
const message = `HTTP error ${response.status}: ${response.statusText}\n${body}`;
72+
super(message);
73+
this.name = this.constructor.name;
74+
Object.setPrototypeOf(this, new.target.prototype);
75+
}
76+
}
5877

5978
export const formatMoney = (amount: number, currency: string) =>
6079
new Intl.NumberFormat("en-US", {

0 commit comments

Comments
 (0)