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 🙏

© 2026 – Pkg Stats / Ryan Hefner

http-exception-transformer

v1.2.3

Published

Always wanted to ensure that your express application to have extensive error handling and expose a structured and reliable API at the same time? This package is written to ensure the same.

Downloads

68

Readme

NodeJS Http Exception Handler

Always wanted to ensure that your express application to have extensive error handling and expose a structured and reliable API at the same time? This package is written to ensure the same.

Features

  • Exceptions for commonly encountered problems like NotFound (404), Internal Server Error (500), Conflicts (409), [...more being added]
  • Just throw errors. No need to wrap codebase into try catch blocks and make codebase redundant.
  • CLean and Easy to use API.

Simple Usage

To add the package to your application, use

yarn -> yarn add http-exception-transformer
npm -> npm install http-exception-transformer

Now in your express.js application, to trigger an exception, throw the error anywhere. This even works when you are calling functions (often called controllers) from the routes handler and throwing exceptions in those functions.

import { HttpExceptionTransformer } from 'http-exception-transformer'
import { BadRequestException } from 'http-exception-transformer/exceptions'

app.get("/id",(req, res)=>{
    if(req.params.id == 0){
        throw new BadRequestException("Only +ve ids accepted")
    }
})

Now add this line when you have attached all the route handlers. Notice that it is very important to place these after adding all routes as hierarchy in which express.js propagates errors.

app.use(HttpExceptionTransformer)

Now the exceptions will return a response like these

{
  "code": 400,
  "error": true,
  "message": "Only +ve ids accepted",
  "reference":"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400"
}

Usage with user defined callback

import { HttpExceptionTransformer } from 'http-exception-transformer'
import { BadRequestException } from 'http-exception-transformer/exceptions'

app.get("/id",(req, res)=>{
    if(req.params.id == 0){
        throw new BadRequestException("Only +ve ids accepted", {}, () => {
            console.log("This function will run after the error is thrown");
        })
    }
})

Optionally, you can configure the error handler to run a callback function that runs after the error is thrown.

Now the exceptions will return a response like these

{
  "code": 400,
  "error": true,
  "message": "Only +ve ids accepted",
  "reference":"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
  "payload": {},
}

And, will also run the function that you defined earlier.

Note: In order to pass in a function, it is mandatory to pass a payload along with it. If you don't want to pass in any payload, simply pass {} as the payload.

API Reference

The middleware and exceptions are designed to give user the flexibility to send data alongwith exceptions, and also the option to set a custom error message if required.

The signature of the exception is the following.

  • the message is optional : if the message is not supplied, then the error message corresponding to the code is automatically shared.
  • the payload is optional : just in case you want to send data alongwith the exceptions, simply pass the data to be shared.
  • the callback is optional : if you want to run a function, simply pass your function to be executed. Defaults to () => null.
SomeException(message?: string, payload?: any, callback: () => void = () => null) {}

The following are completely valid implementations


app.get(......, ()=>{
    if(...){
        throw new NotFoundException();
    }else if(...){
        throw new BadRequestException("Invalid DATA format !")
    }else {
        throw new UnAuthorizedException("Not allowed !", {userID: 101})
    }
})

Development

Since the codebase is redundant, and almost all derived classes have the same structure, we're geneating the exceptions from a json file. Therefore if you wish to make changes in the exception body, please change the codebase in the generators directory.

The data source has the following structure. Feel free to send PRs for adding support for more exception types.

[
    {
      code: 400,
      name: 'BadRequestException',
      message: 'The server could not understand the request due to invalid syntax.',
      reference: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400',
    },
    {
      code: 401,
      name: 'UnAuthorizedException',
      message: 'The client must authenticate itself to get the requested response.',
      reference: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401',
    },
    {
      code: 404,
      name: 'NotFoundException',
      message: 'The server can not find the requested resource',
      reference: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404',
    }
]