Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e.sent.data.forEach is not a function when getting invalid JSON response #3675

Closed
L3P3 opened this issue Mar 10, 2021 · 6 comments
Closed

e.sent.data.forEach is not a function when getting invalid JSON response #3675

L3P3 opened this issue Mar 10, 2021 · 6 comments

Comments

@L3P3
Copy link

L3P3 commented Mar 10, 2021

Describe the bug

I am working with a (not really clean/modern) PHP backend I don't have any control over. When there is a server error, it sometimes responds with bare php error printouts or simple html code, not formatted in JSON. In these cases, we don't get something like a "invalid format" exception from axios we could handle, but instead a cryptic JS error saying e.sent.data.forEach is not a function.
We had this issue from day one, about 2 years ago. Since it is such an apparently obvious bug, we thought that just waiting will fix it. Nope. Wonder why no one else had this issue. 🤔

To Reproduce

axios.post("myapi", ["some", "data", "to", "post"])

Normally, we get JSON responses like this:

{success: true, message: "Transaction done. Good day!"}

Or this:

{success: false, message: "I'm afraid I can't do that, Dave."}

Now, consider this myapi responds with something like this:

<h1>Permission denied</h1>
Please log in to see this page!

It were quite ugly to handle these cases via a SyntaxError and parsing the html alone but it is even uglier to catch a random, undocumented JS error thrown inside axios.

Expected behavior

I expect to get a SyntaxError or something even more dedicated in case a JSON response is not actually valid JSON.

Environment

  • Axios Version 0.19.2
  • Adapter no idea/does not matter
  • Browser any
  • Browser Version any
  • Node.js Version none
  • OS: any
  • Additional Library Versions: none

Additional context/Screenshots

I am afraid I cannot give you any more screenshots than the message already stated in a message box.
Also cannot give you a stack trace for now as I currently do not have time for setting up the app locally.

@DigitalBrainJS
Copy link
Collaborator

Wonder why no one else had this issue.
#61, #3419

@L3P3
Copy link
Author

L3P3 commented Mar 11, 2021

Wonder why no one else had this issue.
#61, #3419

The key difference here is that I get an internal JS error that is not documented whatsoever.

@prashanthgundas
Copy link

prashanthgundas commented Mar 22, 2021

i'm facing same issue getting string data from API. may it is related AXIOS version issue?using "axios": "^0.21.1",

used JSON.parse methos no use for me
image

11
response

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Mar 22, 2021

e.sent.data.forEach is not a function
I get an internal JS error

@L3P3 Hmm, are you sure? Can you share the error stack? The Axios source code doesn't contain the e.sent.data.forEach string, moreover, it's written in ES3, so it doesn't use Array.prototype.forEach method at all.
I'm pretty much sure, this is the error that your handler is throwing. And if so, just check the type of the data field before iterating over it as an array. If Axios cannot parse the JSON, it leaves the response as a string and currently, it doesn't throw any SyntaxError exceptions.

@github-actions
Copy link
Contributor

github-actions bot commented May 3, 2021

Hello! 👋

This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days.

Thanks.

@jasonsaayman
Copy link
Member

Fixed in #3688

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants