这是indexloc提供的服务,不要输入任何密码
Skip to content

Error handling when JSON.parse fails #266

@rasmus0201

Description

@rasmus0201

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions