Skip to content

Instantly share code, notes, and snippets.

@hnry
Created February 1, 2017 01:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnry/c0a153b2df676b4b8ef3109925bc609e to your computer and use it in GitHub Desktop.
Save hnry/c0a153b2df676b4b8ef3109925bc609e to your computer and use it in GitHub Desktop.
catch all unhandled errors or bad responses
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