Skip to content

Commit

Permalink
🐛 Update resolver.ts - Add null verification to .get("Content-Type")
Browse files Browse the repository at this point in the history
The line response.headers.get("Content-Type").split(";")[0] is missing a ? before the call to "split".
The Content-Type header may not be filled if the response has no body (e.g., in responses that only return a status code). When that happens, you get the following error:
TypeError: Cannot read properties of null (reading 'split')
              at file://.../node_modules/wretch/dist/resolver.js:44:88
              at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
              at <your app's call stack>

This verification handles this case.
  • Loading branch information
jsaraiva authored and elbywan committed Feb 27, 2023
1 parent a3bec71 commit a68a524
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
}
return response.text().then((body: string) => {
err.message = body
if (config.errorType === "json" || response.headers.get("Content-Type").split(";")[0] === "application/json") {
if (config.errorType === "json" || response.headers.get("Content-Type")?.split(";")[0] === "application/json") {
try { err.json = JSON.parse(body) } catch (e) { /* ignore */ }
}
err.text = body
Expand Down

0 comments on commit a68a524

Please sign in to comment.