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

Check res.text as well to return a better error txt message #1534

Merged
merged 1 commit into from
Jan 7, 2020

Conversation

AhyoungRyu
Copy link
Contributor

@AhyoungRyu AhyoungRyu commented Dec 4, 2019

Time to time I see my app throws "Unsuccessful HTTP response" error message when it got unsuccessful response even though a backend sent an error message. But sometimes it throws a correct error that I got from the backend. After some investigation, I realized that sometimes there's no value in res.statusText but only res.text has one. For example, below both responses are from same API request.

Case 1

response: {
  method: 'POST',
  url: 'https://test.url/api/v1/createaccount',
  data: { ... },
  headers: { ... },
  text: 'Unauthorized',
  statusText: '', <-- has no value
  statusCode: 401,
  status: 401,
  statusType: 4,
  info: false,
  ok: false,
  clientError: true,
  serverError: false,
  ...,
}

In this case, it always returns "Unsuccessful HTTP response" which is not a helpful message at all even though backend sent frontend a proper error message in the API response

Case 2

response: {
  method: 'POST',
  url: 'https://test.url/api/v1/createaccount',
  data: { ... },
  headers: { ... },
  text: 'Unauthorized',
  statusText: 'Unauthorized', <-- has the same value with `text` field
  statusCode: 401,
  status: 401,
  statusType: 4,
  info: false,
  ok: false,
  clientError: true,
  serverError: false,
  ...,
}

In this case, it throws me a proper error message as I expected

@niftylettuce niftylettuce merged commit d957c81 into ladjs:master Jan 7, 2020
@niftylettuce
Copy link
Collaborator

niftylettuce commented Jan 7, 2020

thanks @AhyoungRyu for your PR, I've published to npm with minor version bump as v5.2.0.

npm install superagent@latest

or with yarn:

yarn add superagent@latest

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

Successfully merging this pull request may close these issues.

2 participants