diff --git a/.changeset/fluffy-lamps-provide.md b/.changeset/fluffy-lamps-provide.md new file mode 100644 index 000000000..8bc10aa93 --- /dev/null +++ b/.changeset/fluffy-lamps-provide.md @@ -0,0 +1,5 @@ +--- +'houdini': patch +--- + +Throw the semantic HTTP error code and message when receiving a non-JSON error response from the server diff --git a/packages/houdini/src/runtime/client/plugins/fetch.ts b/packages/houdini/src/runtime/client/plugins/fetch.ts index e1b92ab34..4809e1e94 100644 --- a/packages/houdini/src/runtime/client/plugins/fetch.ts +++ b/packages/houdini/src/runtime/client/plugins/fetch.ts @@ -94,6 +94,17 @@ const defaultFetch = ( }, }) + // Avoid parsing the response if it's not JSON, as that will throw a SyntaxError + if ( + !result.ok && + result.headers.get('content-type') !== 'application/json' && + result.headers.get('content-type') !== 'application/graphql+json' + ) { + throw new Error( + `Failed to fetch: server returned invalid response with error ${result.status}: ${result.statusText}` + ) + } + return await result.json() } }