Skip to content

DevSpeedjs/devSpeed-errorhandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published