-
-
Notifications
You must be signed in to change notification settings - Fork 102
Description
Line 77 in bf1dfe8
err.json = JSON.parse(body) |
Issue: .errorType('json')
fails when non-JSON response is returned
If the server does not return JSON (e.g. it returns HTML or empty response), and .errorType('json')
is configured, Wretch currently throws an exception during JSON.parse. This causes the original response context to be lost, and the resulting WretchError is not thrown.
In my case, I’m working with a backend I don't fully control, and it occasionally returns HTML error pages. When that happens, the parsing fails and I'm unable to inspect the response properly in my error handler.
Suggestion:
Would it be possible to wrap the JSON parsing in a try/catch block internally, so that even if parsing fails, the WretchError is still thrown, providing access to the original response.
This would make .errorType('json')
more robust for real-world use cases where server behavior isn't always predictable.
Thanks for the great library!