A zero dependency tiny module to handle HTTP errors which extends error nodejs error.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
const { AccessDeniedError } createError = require('api-errors')
var express = require('express')
var app = express()
app.use(function (req, res, next) {
if (!req.user) return next(new AccessDeniedError('Please login to view this page.')) // message is optional
next()
})
app.use(function(err, req, res, next) {
// catch all else api errors
if (error instanceof APIError) {
return res
.status(error.status)
.send({
status: false,
message: error.message,
});
}
})
This is the current API, currently extracted from Koa and subject to change.
Status Code | Constructor Name |
---|---|
400 | BadRequestError |
401 | UnauthorizedError |
403 | ForbiddenError |
404 | NotFoundError |
405 | MethodNotAllowedError |
409 | ConflictError |
412 | PreconditionFailedError |
415 | UnsupportedMediaTypeError |
422 | UnprocessableEntityError |
500 | InternalServerError |