Skip to content

Commit

Permalink
Give better error when receiving non-JSON response (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mtn-View authored Dec 18, 2024
1 parent 807bb2b commit d6375b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-lamps-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini': patch
---

Throw the semantic HTTP error code and message when receiving a non-JSON error response from the server
11 changes: 11 additions & 0 deletions packages/houdini/src/runtime/client/plugins/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit d6375b6

Please sign in to comment.