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

Commit f6c0e21

Browse files
committed
feat: send errors as trailer headers
1 parent 2ff8312 commit f6c0e21

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/utils/progress-stream.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ class ProgressStream extends Transform {
4242
return callback()
4343
}
4444

45-
if (typeof chunk.Message !== 'undefined' ||
46-
typeof chunk.Code !== 'undefined') {
47-
this.emit('error', chunk)
48-
}
49-
5045
callback(null, chunk)
5146
}
5247
}

src/utils/request-api.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ function onRes (buffer, cb) {
4848
// Return a stream of JSON objects
4949
if (chunkedObjects && isJson) {
5050
const outputStream = pump(res, ndjson.parse())
51+
res.on('end', () => {
52+
// if we got an error here, we have to throw
53+
// a) we can't `outputStream.destroy(err)` because its already closed
54+
// b) the callback has been already called, no point in calling it again
55+
let err = res.trailers['x-stream-error']
56+
if (err) {
57+
err = JSON.parse(err)
58+
const error = new Error(`Server responded with 500`)
59+
error.code = err.Code
60+
error.message = err.Message
61+
throw error
62+
}
63+
})
5164
return cb(null, outputStream)
5265
}
5366

0 commit comments

Comments
 (0)