From 62d733e9b9dc7a73de5f63275215faf85e5ac45b Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 24 Nov 2017 16:32:18 -0600 Subject: [PATCH] fix: propagate trailer errors correctly (#636) * fix: propagate trailer errors correctly * test: fix CRLFs for fixture files --- appveyor.yml | 3 +++ src/utils/send-files-stream.js | 2 ++ src/utils/send-request.js | 16 ++-------------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index b3d477011..ba93339ba 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,6 +3,9 @@ environment: - nodejs_version: "6" - nodejs_version: "8" +init: + - git config --global core.autocrlf input + # cache: # - node_modules diff --git a/src/utils/send-files-stream.js b/src/utils/send-files-stream.js index 0a40c6445..dda4f8051 100644 --- a/src/utils/send-files-stream.js +++ b/src/utils/send-files-stream.js @@ -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) diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 336e356d2..3ab2b57e9 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -47,12 +47,6 @@ 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) { @@ -60,15 +54,9 @@ function onRes (buffer, cb) { 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)