Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: propagate trailer errors correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Nov 23, 2017
1 parent 447f362 commit dfee6e5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/utils/send-files-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ module.exports = (send, path) => {
return
}

response.on('error', (err) => retStream.emit('error', err))

response.on('data', (d) => {
if (d.Bytes && options.progress) {
options.progress(d.Bytes)
Expand Down
16 changes: 2 additions & 14 deletions src/utils/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,16 @@ function onRes (buffer, cb) {
// Return a stream of JSON objects
if (chunkedObjects && isJson) {
const outputStream = pump(res, ndjson.parse())
// TODO: This needs reworking.
// this is a chicken and egg problem -
// 1) we can't get Trailer headers unless the response ends
// 2) we can't propagate the error, because the response stream
// is closed
// (perhaps we can workaround this using pull-streams)
res.on('end', () => {
let err = res.trailers['x-stream-error']
if (err) {
// Not all errors are JSON
try {
err = JSON.parse(err)
} catch (e) {
err = {
Code: 'n/a',
Message: err
}
err = { Message: err }
}
const error = new Error(`Server responded with 500`)
error.code = err.Code
error.message = err.Message
outputStream.destroy(error) // error is not going to be propagated
outputStream.emit('error', new Error(err.Message))
}
})
return cb(null, outputStream)
Expand Down

0 comments on commit dfee6e5

Please sign in to comment.