Created
November 26, 2018 15:52
-
-
Save queerviolet/e3b24a903fcb92d4a2f965836c1ffac5 to your computer and use it in GitHub Desktop.
Express error handling
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
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