1.0.1 • Published 2 years ago

@colussi/errors v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

@colussi/errors

npm version CI Maintainability Test Coverage

Install

$ npm install --save-dev @colussi/errors

Errors

Errors exposed in this package extends the Error class, and have a status property containing the HTTP equivalent status. The error message defaults to a description of the HTTP status.

NameStatus CodeStatus NameParameters
AuthenticationError401UNAUTHORIZEDmessage
AuthorizationError403FORBIDDENmessage
BadGatewayError502BAD_GATEWAYmessage
ConflictError409CONFLICTmessage
CustomError400BAD_REQUESTmessage
InternalServerError500INTERNAL_SERVER_ERRORmessage
LockedError423LOCKEDmessage
NotFoundError404NOT_FOUNDmessage
RequestError400BAD_REQUESTmessage
ServiceUnavailableError503SERVICE_UNAVAILABLEmessage
TimeoutError504GATEWAY_TIMEOUTmessage
TooManyRequestsError429TOO_MANY_REQUESTSmessage
UnprocessableEntityError422UNPROCESSABLE_ENTITYmessage

example

if (!product) {
  throw new NotFoundError('Product not found');
}

results in

[404] {
  "type": "NotFoundError",
  "message": "Product not found"
}

Middlewares

  • The middleware notFoundMiddleware will handle the error and respond to the request in a standardized way.

  • The notFoundMiddleware middleware will respond to all routes that are not specified

To use in an express application, register the middlewares after the routes:

import Express from 'express';
import { errorHandler, notFoundMiddleware } from '@colussi/errors';

const app = new Express();

app.use(...); // your routes

app.use(errorHandler);
app.all('*', notFoundMiddleware);