1.0.12 • Published 4 years ago

@salma-dev/error-handler v1.0.12

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Error handler middleware

the error handler can be used to handle different http errors

to install:

npm i @salma-dev/error-handler

to integerate it with your express:

commonjs:

const { RestApiErrorHandler } = require('@salma-dev/error-handler')

const app = require('express')()
..
  //app config
..

app.use(RestApiErrorHandler)

the handler will catch any error that is thrown inside your app routes

you can import different errors to throw in your app:

ErrorDescriptionStatus code
NotFoundErrorused to handle not found resource404
BadRequestused to handle invalid data400
NotAuthorizedErrorto deny authorization on some resource401
ForbiddenErrorwhen the user is authenticated but has no permission to use the resource403
ConflictErrorsometimes used to prevent data duplication409
RequestValidationErrorthis is useful is if you use express-validator, it takes the errors array and returns another with status400

you can import any of these errors and throw in your route handler like this:

const {NotFoundError}  = require('@salma-dev/error-handler')

...

//some code 

..


app.get('/resource/:id', function(req, res){
   //some code

   throw new NotFoundError()
})

this will cause the handler to return response with body like this:

{
    "errors": [{"message": "Not Found"}]
}
1.0.12

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago