Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: handle errors as trailer headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Oct 12, 2017
1 parent 480896a commit 0a7092d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/http/api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ exports.add = {
reply(stream)
.header('x-chunked-output', '1')
.header('content-type', 'application/json')
.header('Trailer', 'X-Stream-Error')

function _writeErr (msg, code) {
const err = JSON.stringify({ Message: msg, Code: code })
request.raw.res.addTrailers({
'X-Stream-Error': err
})
return aborter.abort()
}

pull(
fileAdder,
Expand All @@ -251,19 +260,11 @@ exports.add = {
}),
pull.collect((err, files) => {
if (err) {
replyStream.push({
Message: err,
Code: 0
})
return aborter.abort()
return _writeErr(err, 0)
}

if (files.length === 0 && filesParsed) {
replyStream.push({
Message: 'Failed to add files.',
Code: 0
})
return aborter.abort()
return _writeErr('Failed to add files.', 0)
}

files.forEach((f) => replyStream.push(f))
Expand Down

0 comments on commit 0a7092d

Please sign in to comment.