devspeed-errorhandler
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@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"
  }
}

Package Sidebar

Install

npm i devspeed-errorhandler

Weekly Downloads

8

Version

1.0.1

License

MIT

Unpacked Size

16.2 kB

Total Files

8

Last publish

Collaborators

  • silentwolf-dev