express-default-errors
Default ExpressJS error-handling middleware.
Installation
$ npm install express-default-errors
Usage
express-default-errors
provides simple middleware to catch default errors and serve custom errors.
var express = require('express')
var errorHandler = require('express-default-errors')
var app = express()
var router = express.Router()
router.get('/', function (req, res, next) {
res.status(200)
res.send('success')
})
router.get('/restricted-route', function (req, res, next) {
var err = new Error()
err.status = 401
next(err)
})
router.get('/error-route', function (req, res, next) {
var err = new Error('A custom error was caused.')
next(err)
})
router.get('/invalid-status-code-error-route', function (req, res, next) {
var err = new Error()
err.status = 299
next(err)
})
app.use(router)
app.use(errorHandler())
app.listen(3000)
Default Errors
Setting the status code will cause the middleware to serve default error messages when calling new Error()
without passing in any arguments, or if err.message
is not set.
Status Code | Default Message |
---|
400 | Bad Request |
401 | Unauthorized |
404 | Not Found |
500 | Internal Server Error |
License
MIT