npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

error-handler-module

v1.0.7

Published

This module provides handling error methods for different systems.

Downloads

1,758

Readme

error-handler-module

npm CircleCI

This module provides a way to handle error in an express app with a few methods that creates error and an express error middleware handleHttpError.

  1. Installation
  2. Basic Error types
  3. Methods
  1. Implementation example
  2. How to debug the errors

Installation

npm install --save error-handler-module

Basic Error types

We have setup some errors that we use often use in our projects and we store them in an object which is this:

const CustomErrorTypes = {
  BAD_REQUEST: 'bad_request',
  FORBIDDEN: 'forbidden',
  NOT_FOUND: 'not_found',
  OAS_VALIDATOR: 'OpenAPIUtilsError:response',
  SWAGGER_VALIDATOR: 'swagger_validator', // Deprecated
  UNAUTHORIZED: 'unauthorized',
  WRONG_INPUT: 'wrong_input',
};

NOTE If you need another kind of error keep reading on tagError method.

Methods

errorFactory(type: String) => (message: String)

This function creates a custom Error with a type and a message you decide.

Example

const { errorFactory, CustomErrorTypes } = require('error-handler-module');

// With Basic Errors
const wrongInputError = errorFactory(CustomErrorTypes.WRONG_INPUT);
wrongInputError('Error message');

/* returns
  CustomError {
    name: 'CustomError',
    type: 'wrong_input',
    message: 'Error message'
  }
*/

// with your custom types

// With Basic Errors
const customTypeError = errorFactory('custom-type');
customTypeError('Error message');

/* returns
  CustomError {
    name: 'CustomError',
    type: 'custom-type',
    message: 'Error message'
  }
*/

handleHttpError(logger: Object, metrics: Object) => (err, req, res, next)

This is a function which will work as a middleware for your express app so that your errors will response with an HTTP response.

You have to pass logger and metrics objects as parameters (Metrics is not required).

Example

const express = require('express');
const { handleHttpError } = require('error-handler-module');

const app = express();

app.use(handleHttpError(logger, metrics));

tagError(error: Error, newTypes: Object)

This function is going to tag your errors from CustomError to HTTPErrorsso that handleHttpError middleware will understand them.

It receives error and newTypes (not required)

NewTypes object

In order to add new error types, this object must match this structure:

const newTypes = {
  <Error_type_string>: <status_code_number>,
  <Error_type_string>: <status_code_number>,
  ...
};

Example

const { errorFactory } = require('error-handler-module');

// With Custom Errors
const customTypeError = errorFactory('tea-pot-error');
const error = customTypeError('Error message');

// const new Type
const newTypes = {
  'tea-pot-error': 418,
};


tagError(error, newTypes);
/* This returns
  CustomHTTPError {
    name: 'CustomHTTPError',
    statusCode: 418,
    message: 'Error message',
    extra: undefined
  }
*/
// This tagError will be added to the catch of the enpoints like this
return next(tagError(error, newTypes))

// With Basic Errors

// With Custom Errors
const customTypeError = errorFactory(CustomErrorTypes.WRONG_INPUT);
const error = customTypeError('Error message');

tagError(error);
/* This returns
  CustomHTTPError {
    name: 'CustomHTTPError',
    statusCode: 400,
    message: 'Error message',
    extra: undefined
  }
*/

Implementation example

const express = require('express');
const {
  CustomErrorTypes,
  errorFactory,
  handleHttpError,
  tagError,
} = require('error-handler-module');

const app = express();

const loggerMock = {
  error: () => '',
};

app.get('/test-error-basic', (req, res, next) => {
  // creating tagged error
  const wrongInputError = errorFactory(CustomErrorTypes.NOT_FOUND);
  try {
    throw wrongInputError('Wrong Input message');
  } catch (error) {
    return next(tagError(error));
  }
});

app.get('/test-error-extended', (req, res, next) => {
  // creating a custom tag error
  // for example custom db-error
  const dbError = errorFactory('db-access-not-allowed');
  /*
   * New types must be objects with error and a valid status code
   */
  const newErrors = {
    'db-access-not-allowed': 401,
  };
  try {
    throw dbError('db Error');
  } catch (error) {
    return next(tagError(error, newErrors));
  }
});

app.use(handleHttpError(loggerMock));

module.exports = app;

Debug

This project uses Debug if you want more info of your error please run DEBUG=error-handler-module <Your_npm_script>.