Skip to content

Bug: Axios throws unclear error when JSON.parse fails on malformed responses #7224

@mahdiehanjomshoae

Description

@mahdiehanjomshoae

Describe the bug

Describe the bug
When Axios receives a response with malformed JSON, the
error thrown is not descriptive enough to identify the underlying issue.
The current error stack does not indicate that the failure originated
from JSON.parse, making debugging harder for users.

To Reproduce
Steps to reproduce the behavior: 1. Set up a mock server
that returns an invalid JSON response (e.g., a trailing comma or missing
brace). 2. Perform a GET request using Axios: `
``js
axios.get(‘http://localhost:3000/bad-json’)

 3. Observe the thrown error message.

Expected behavior Axios should: - Detect that the error is caused by a
JSON.parse failure. - Show a clearer error message, such as: “Failed to
parse JSON response. Response was not valid JSON.”

Environment 
- Windows 11 
- Node.js v20+ 
- Axios latest version

Additional context Improving error clarity for malformed JSON responses
would help developers diagnose backend issues faster.

### To Reproduce

_No response_

### Code snippet

```js

Expected behavior

No response

Axios Version

No response

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

No response

OS

No response

Additional Library Versions

Additional context/Screenshots

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue::bugThis issue is related to a bug that requires fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions