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

responseType: stream, does not return response data for 4xx-5xx errors #1927

Closed
unerh opened this issue Dec 13, 2018 · 2 comments
Closed

responseType: stream, does not return response data for 4xx-5xx errors #1927

unerh opened this issue Dec 13, 2018 · 2 comments

Comments

@unerh
Copy link

unerh commented Dec 13, 2018

Summary

The following server response (which is returned by postman)
Status: 4xx-5xx

{
  status: 500,
  message: 'Call your admin'
}

Is not present in the catch, when called with:

axios('URL', {
    responseType: 'stream',
    headers: {
      'Content-Type': 'application/json',
    },
})
.then(response => {
  response.data.pipe('Destination Stream');
})
.catch(error => console.log(error.response));

I only get back the status code, and not the response data (as printed above) back. I'm aware that for responseType: 'stream' the error.response.data is meant to be the actual stream. But I can't find the response message anywhere else in the error.response structure.

Doesn't axios return this back when it errors for streams?

Context

  • axios version: v0.18.0
  • Environment: node v8.6.0
@unerh
Copy link
Author

unerh commented Dec 19, 2018

As this is a stream, the exact error message can be deciphered from the stream buffer if really needed. We no longer need to return this error so I'm closing this issue.

But as an enhancement it might be good to extract error response message out of the buffer into the main error.response object.

@unerh unerh closed this as completed Dec 19, 2018
@bam-tbf
Copy link

bam-tbf commented Feb 10, 2020

I would recommend handing these types of errors automatically. Otherwise the standard error handling functionality of Axios does not work as expected.

For those looking for a quick example of a solution in the meantime you can do something like:

try {
  const response = await axios({ method: 'GET', url, responseType: 'stream' })
} catch (err) {
    let streamString = ''
    err.response.data.setEncoding('utf8')
    err.response.data
      .on('data', (utf8Chunk) => { streamString += utf8Chunk })
      .on('end', () => {
        d.error(streamString, 'Stream error')
      })
  }

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants