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

Server response body is swallowed if code is not OK #234

Closed
happycollision opened this issue Jun 9, 2018 · 4 comments
Closed

Server response body is swallowed if code is not OK #234

happycollision opened this issue Jun 9, 2018 · 4 comments

Comments

@happycollision
Copy link

Pretty sure this was introduced at commit 7558b36

Since v4.0.0, if a server responds with something like this (with a non-ok response code):

{errors: [{
  status: 401,
  title: 'no user exists with that email and password combination',
}]}

The errors array disappears and the only thing available in the catch block of the call to authenticate() is a vanilla error where message is "Unauthorized".

Given the following:

this.get('session').authenticate('authenticator:jwt', badCredentials)
  .catch(e => console.log(e));

3.x console:

{
  errors: [
    {
      status: 401,
      title: 'no user exists with that email and password combination',
    }
  ]
}

Post 4.0.0 console:

Error: Unauthorized
    at then.response (token.js:92)
    at tryCatcher (rsvp.js:215)
    at invokeCallback (rsvp.js:393)
    at publish (rsvp.js:379)
    at rsvp.js:14
    at invoke (backburner.js:205)
    at Queue.flush (backburner.js:125)
    at DeferredActionQueues.flush (backburner.js:278)
    at Backburner.end (backburner.js:410)
    at Backburner._boundAutorunEnd (backburner.js:372)
@happycollision happycollision changed the title JSON:API errors are swallowed Server response body is swallowed if code is not OK Jun 9, 2018
@fenichelar
Copy link
Owner

fenichelar commented Jun 9, 2018

@happycollision How does that look? In your example, the console output would be:

{
   "statusText": "Unauthorized",
   "status": 401,
   "json": {
      "errors": [
         {
            "status": 401,
            "title": "no user exists with that email and password combination"
         }
      ]
   }
}

@happycollision
Copy link
Author

That works for me just fine.

But are there scenarios where the server might not be sending JSON? Perhaps the error messages will be in plain text, etc. That discussion is up to you and your fellow collaborators.

In any case, thanks for that amazingly fast response!

@fenichelar
Copy link
Owner

fenichelar commented Jun 9, 2018

Yeah, I thought about. It was kind of messy so I figured it wasn't worth it but you are right. I added support for both: 32568ba

JSON response:

{
  statusText: "Unauthorized",
   status: 401,
   text: "{"errors":[{"status":401,"title":"no user exists with that email and password combination"}]}",
   json: {
      "errors": [
         {
            "status": 401,
            "title": "no user exists with that email and password combination"
         }
      ]
   }
}

Text response:

{
  statusText: "Unauthorized",
   status: 401,
   text: "no user exists with that email and password combination"
}

@happycollision
Copy link
Author

Nice!

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