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

How to handle error response with failure status? #221

Open
AnshulM34 opened this issue Jul 9, 2019 · 1 comment
Open

How to handle error response with failure status? #221

AnshulM34 opened this issue Jul 9, 2019 · 1 comment

Comments

@AnshulM34
Copy link

AnshulM34 commented Jul 9, 2019

Below is something I tried using

trae.create({baseUrl: '/api'})
  .post('some url')
  .then(do some operation)
  .catch(err)

I don't get error response in catch block... I just get Unexpected end of JSON input

I want to handle 404 , 400, 500

This may not be issue... It is something I am facing.

@gillchristian gillchristian changed the title How can i fetch error response like(404, 400) it does not come in catch block How to handle error response with failure status? Aug 7, 2019
@gillchristian
Copy link
Member

@AnshulM34 thanks for reporting. I formatted a bit your message, added syntax highlighting. I suggest you keep that in mind when opening and issue, it things easier to understand for us maintainers.

It's a bit hard to reproduce your issue because we lack the context. If you would provide and example in Codesandbox.

That said, I have a guess of what's causing your issue. We also parse the response when there's an error. Seeing "Unexpected end of JSON input" we are using the json parser when we shouldn't. At the moment I'm not sure if that could be changed, but we'll improve it for sure on the v2

Here's the code:

function defineReader(ok, headers, reader) {
if (ok && validateReader(reader)) {
return reader;
}
const contentType = headers.get('Content-Type') || '';
return contentType.includes('application/json')
? 'json'
: 'text';
}

You can check the headers of the response of your API, maybe it says application/json when it shouldn't.

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

2 participants