0.0.6 • Published 8 years ago

@internap/base-http-errors v0.0.6

Weekly downloads
2
License
MIT
Repository
-
Last release
8 years ago

Base HTTP Errors

A set of basic HTTP errors that can be used in Express.js

Usage

const {
    NotFound,
    BadRequest
} = require('base-http-errors');

app.use((req, res, next) => {
    if (routeDoesntExist()) {
        next(new NotFound());
    }
    
    if (somethingWrongWithRequest()) {
        next(new BadRequest('A Custom Message'));
    }
});


// error handler
app.use((err, req, res, next) => {
    res.status(err.status);
    res.json({
        message: err.message,
        type: err.constructor.name,
    });
});

Available Errors

Error ClassDefault MessageStatus Code
BaseErrorBase Error500
TooManyRequestsToo Many Requests429
NotFoundNot Found404
UnauthorizedUnauthorized401
ForbiddenForbidden403
BadRequestBad Request400
InvalidInputInvalid Input400

Custom Errors

You may subclass any of the above listed errors to create your own error.

E.g.

class CustomError extends BaseError {
    constructor(message) {
        super(message || 'Custom Error');
        this.status = 400;
    }
}