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 (#636)
Browse files Browse the repository at this point in the history
* fix: propagate trailer errors correctly

* test: fix CRLFs for fixture files
  • Loading branch information
dryajov authored and daviddias committed Nov 24, 2017
1 parent 447f362 commit 62d733e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
3 changes: 3 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ environment:
- nodejs_version: "6"
- nodejs_version: "8"

init:
- git config --global core.autocrlf input

# cache:
# - node_modules

Expand Down
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 62d733e

Please sign in to comment.