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

express-custom-error-manager

v1.0.6

Published

ExpressJS package for handling and managing custom error responses.

Downloads

1

Readme

express-custom-error-manager

"express-custom-error-manager" is a minimalistic express.js package for handling and managing custom error responses anywhere in Express application. Simplifying error handling by allowing you to create custom exceptions with specific messages, HTTP status codes, and optional error-response behaviors. With integrated configurable options, you can control how errors are handled, whether to gracefully send error responses or trigger specific actions by using callbacks.

Installation

npm install express-custom-error-manager

Also make sure that you have express.js to use it.

Usage

Import and register the module.

make sure to import and call ErrManager.register() method on the top of the application, before you regiter any routes.

const app = express();

import ErrManager from 'express-custom-error-manager';
app.use(ErrManager.register());

Register ErrManager middleware.

make sure that Errmanager.middleware() will be placed as last middleware of the app.

const app = express();

//...other middleware and routes ...

import ErrManager from 'express-custom-error-manager';
app.use(ErrManager.middleware());

API

ExHandler.createException(message, callback, options)

create custom exception class.

  • message (string): The error message for this exception which will be sent to user.
  • callback(arg) (function(arg)): The callback function which executes when exception is thrown (optional).
  • options ({ httpStatus: 400 ; killProcess: false;}): options object to fine tune exception behavior:

httpStatus sets the status code which will be sent in error message response (by default its 400). killProcess controls if node process should be exited gracefully after error is thrown(by deafult its false).

Example createException usage:

//exceptions/customEx.js/ts
import ErrManager from 'express-custom-error-manager';

const customErr = ErrManager.createException(
  //error message by default its ''
  'error message that will be sent to user',

  // optional callback which can be both sync/async which will be executed when exception will be thrown.
  // we can add parameter in case when we want to pass in data when we throw the exception.
  // example: return customErr('callback arg')  --> 'callback is called with data: callback arg'
  (data) => {
    console.log('callback is called with data: ', data);
  },
  // optional options object and their properties to fine tune custom exception
  {
    httpStatus: 200, // defaults to 400
    killProcess: false // defaults to false
  }
);

Error response format

{
  "message": "error message",
  "httpStatus": 400
}

Example

You can return your custom error anywhere in the application and and it will directly skip to ErrrHandler middleware which whill execute callback if its provided and return response message to the user and close the connection.

import express from 'express';
import ErrManager from 'express-custom-error-manager';

const app = express();

app.use(ErrManager.register());

const userNotFounderr = ErrManager.createException(
  'user not found',
  (userId) => {
    console.log(`user with ${userId} not found`);
  },
  {
    httpStatus: 404
  }
);

app.get('/user/:id', (req, res, next) => {
  const userId = req.params.id;
  if (!isValidUserId(userId)) {
    return userNotFound(userId);
  }
  // ...rest of the route handler...
});

app.use(ExHandler.middleware());

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

License

This project is licensed under the MIT License.