Created
February 1, 2017 01:36
-
-
Save hnry/c0a153b2df676b4b8ef3109925bc609e to your computer and use it in GitHub Desktop.
catch all unhandled errors or bad responses
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
const spirit = require("spirit").node | |
const catchall = (handler) => (request) => handler(request).then((resp) => { | |
// something beforehand gave a bad response | |
// set our own custom message | |
if (!spirit.is_response(resp)) { | |
return { status: 500, headers: {}, body: "generic error message because of bad response" } | |
} | |
return resp | |
// something beforehand threw an error | |
// set a custom catch all message | |
}).catch((err) => { | |
return { status: 500, headers: {}, body: "generic error message because of thrown error" } | |
}) | |
http.createServer(spirit.adapter(app, [catchall, other_middleware, ...])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment