diff --git a/src/networkRequest.ts b/src/networkRequest.ts index 95fead0..3b4695c 100644 --- a/src/networkRequest.ts +++ b/src/networkRequest.ts @@ -6,5 +6,19 @@ export const networkRequest = async ( url: string, ): Promise> => { const result = await fetch(url); + + const { status, headers } = result; + + if (status < 200 || status >= 300) { + const body = await result.text(); + throw new Error(`HTTP request failed (${status}): ${body}`); + } + + const contentType = headers.get('content-type'); + if (contentType?.includes('application/json') === false) { + const body = await result.text(); + throw new Error(`HTTP response is not JSON but ${contentType}: ${body}`); + } + return result.json(); };