Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle promises in the error handler with the same logic of normal ha…
…ndlers (#1134)
- Loading branch information
Showing
4 changed files
with
61 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict' | ||
|
||
function wrapThenable (thenable, reply) { | ||
thenable.then(function (payload) { | ||
// this is for async functions that | ||
// are using reply.send directly | ||
if (payload !== undefined || (reply.res.statusCode === 204 && reply.sent === false)) { | ||
// we use a try-catch internally to avoid adding a catch to another | ||
// promise, increase promise perf by 10% | ||
try { | ||
reply.send(payload) | ||
} catch (err) { | ||
reply.sent = false | ||
reply._isError = true | ||
reply.send(err) | ||
} | ||
} else if (reply.sent === false) { | ||
reply.res.log.error(new Error(`Promise may not be fulfilled with 'undefined' when statusCode is not 204`)) | ||
} | ||
}, function (err) { | ||
reply.sent = false | ||
reply._isError = true | ||
reply.send(err) | ||
}) | ||
} | ||
|
||
module.exports = wrapThenable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters