An HTTP Error collection to use in your next REST API.
npm install --save throw.js
const express = require('express');
const app = express();
const errors = require('throw.js');
const logger = require('winston');
app.get('/', (req, res, next) => {
next(new errors.NotFound());
});
app.use((err, req, res, next) => {
logger.error(err);
if (req.app.get('env') !== 'development' &&
req.app.get('env') !== 'test') {
delete err.stack;
}
res.status(err.statusCode || 500).json(err);
});
app.listen(3000);
const errors = require('throw.js');
errors.CustomError(slug, message, statusCode, errorCode);
Parameters:
- slug[optional]: A unique identifier of this error
- message[optional]: Detailed message of this error
- statusCode[optional]: The HTTP Status number to return
- errorCode[optional]: An internal unique code identifier of this error
All of the classes below have all parameters set up by default, based on RFC7231.
But you can override the message
and the errorCode
to fit your for personal needs.
errors.BadRequest(message, errorCode);
errors.Unauthorized(message, errorCode);
errors.PaymentRequired(message, errorCode);
errors.Forbidden(message, errorCode);
errors.NotFound(message, errorCode);
errors.MethodNotAllowed(message, errorCode);
errors.NotAcceptable(message, errorCode);
errors.ProxyAuthenticationRequired(message, errorCode);
errors.RequestTimeout(message, errorCode);
errors.Conflict(message, errorCode);
errors.Gone(message, errorCode);
errors.UnprocessableEntity(message, errorCode);
errors.FailedDependency(message, errorCode);
errors.InternalServerError(message, errorCode);
errors.NotImplemented(message, errorCode);
errors.BadGateway(message, errorCode);
errors.ServiceUnavailable(message, errorCode);
errors.GatewayTimeout(message, errorCode);
errors.HttpVersionNotSupported(message, errorCode);
errors.NetworkAuthenticationRequired(message, errorCode);
- Implement more Error Classes