Skip to content

Instantly share code, notes, and snippets.

@queerviolet
Created November 26, 2018 15:52
Show Gist options
  • Save queerviolet/e3b24a903fcb92d4a2f965836c1ffac5 to your computer and use it in GitHub Desktop.
Save queerviolet/e3b24a903fcb92d4a2f965836c1ffac5 to your computer and use it in GitHub Desktop.
Express error handling
class BadError extends Error {
get type() { 'very bad' }
}
require('express')()
// Express will convert this synchronous throw
.get('/', (_req, res) => {
throw new BadError('threw an error: failed')
})
// Into a pipeline error, handled here:
.use((err, _req, res, _next) =>
res.send(`[${err.constructor.name}] ${err.message}`))
.listen(9998)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment