From d6375b6b04ecd48fba663e83b3aed9b6b25e51b3 Mon Sep 17 00:00:00 2001 From: Charles Kaup Date: Wed, 18 Dec 2024 03:01:01 -0600 Subject: [PATCH] Give better error when receiving non-JSON response (#1404) --- .changeset/fluffy-lamps-provide.md | 5 +++++ packages/houdini/src/runtime/client/plugins/fetch.ts | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .changeset/fluffy-lamps-provide.md 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() } }