devspeed-errorhandler
v1.0.1
Published
express Error handler
Downloads
1
Maintainers
Readme
@devspeed/errorhandler is an Express Error handler that allows you to handle errors easer. handleError()
and the ErrorResponse()
class
let start with the handleError()
function
handleError()
is a middleware and should be your last middleware for it to work. handleError()
catch errors and send a json response that looks like this
{
"success": false,
"Error": {
"status": 500,
"message": "internal error",
}
}
handleError
should be your last middleware
const express = require('express')
const app = express()
const {ErrorResponse, handleError} = require('@devspeed/errorhandler')
app.use(handleError)
app.listen(3000)
the ErrorResponse()
class allow you to create custom errors using the next
method which than Gets catch by the handleError()
function
Example
app.get('/api/test', (req, res, next)=>{
next(new ErrorResponse(404, 'not found'))
/*
or
next(ErrorResponse.Error(404, 'not found'))
*/
})
the response look like this
{
"success": false,
"Error": {
"status": 404,
"message": "not found"
}
}