@vp_solutions/errors

Organize error handling in your project in easy and clear way.

Usage no npm install needed!

<script type="module">
  import vpSolutionsErrors from 'https://cdn.skypack.dev/@vp_solutions/errors';
</script>

README

Package provide you functionality for organize error handling in your Express project in easy and clear way.

Usage:

import * as express from 'express';
import { BadRequest, getExpressErrorHandler } from '@vp_solutions/errors';

const app = express();

// Throw error of type which suit to your business logic.
app.use((req, res, next) => {
  if (req.params.id !== undefined) {
    return next();
  }

  throw new BadRequest();
});

// Add error handler from package, and your exception will be handled in correct way.
app.use(getExpressErrorHandler());

Function getExpressErrorHandler accept one optional parameter - logger, and return middleware type of ErrorRequestHandler from Express. You can pass instance of logger witch must be implemented according to the interface ILogger from @vp_solutions/errors package.

Given from getExpressErrorHandler handler works with next errors from @vp_solutions/errors package:

  • NotFound
  • Conflict
  • Forbidden
  • BadRequest
  • BadGateway
  • Unauthorized
  • NotAcceptable
  • RateLimitError
  • GatewayTimeout
  • ValidationError
  • MethodNotAllowed
  • ServiceUnavailable
  • InternalServerError
  • NotImplementedError
  • UnsupportedMediaType

Using this error types combine with error handler from package, you organize error handling in your project in easy and clear way. Also you can implement your own error handler with error types from package.

Package provide enum with http status codes (forwarding @vp_solutions/http-statuses). Using this enum you can process error on back-end and front-end side.