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

backend-error-handler

v1.0.1

Published

This documentation provides an overview of the error-handling mechanisms implemented in the backend. It introduces a structured approach for managing errors in an Express.js application, ensuring consistent and efficient error responses.

Readme

Backend Error Handling Package Documentation

This documentation provides an overview of the error-handling mechanisms implemented in the backend. It introduces a structured approach for managing errors in an Express.js application, ensuring consistent and efficient error responses.


Key Components

1. ApiResponse Class

The ApiResponse class standardizes the format of all responses sent to the client.

Example Usage:

For successful responses:

return res.status(200).json(new ApiResponse(200, response, "User Create Successful"));

2. asyncHandler Function

The asyncHandler function wraps Express.js controller functions, allowing asynchronous errors to be caught and passed to the global error handler without explicitly using try-catch blocks in every controller.

Example Usage:

import { asyncHandler } from "backend-error-handler";

const createUser = asyncHandler(async (req: Request, res: Response) => {
    const data = req.body.data;

    const response = await userService.createUser(data);

    return res.status(200).json(new ApiResponse(200, response, "User Create Successful"));
});

export { createUser };

3. expressErrorHandler Function

The expressErrorHandler function is the global error handler. It ensures that all errors, regardless of their origin, are handled and sent to the client in a consistent format.

Global Middleware Setup:

import { expressErrorHandler } from "backend-error-handler";

// Global error handler
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
    expressErrorHandler(err, req, res, next);
});

Error Response Format:

All error responses are sent in the following JSON structure:

{
    "status": <HTTP_STATUS_CODE>,
    "data": {
        "message": "Detailed error message",
        "error": "Error details",
        "errors": ["Additional error messages"]
    },
    "isError": true
}

4. AppError Class

The AppError class provides a structured way to define and throw custom application errors. This supports different types of errors such as database errors, validation errors, and more.

Example Usage:

import { AppError } from "backend-error-handler";

// Throwing validation errors
throw AppError.zodError("Custom Message",error);

// Throwing MongoDB-specific errors
throw AppError.mongoError("Custom Message", error);

// Throwing Prisma-specific errors
throw AppError.prismaError("Custom Message", error);

// Throwing other types of errors - just pass the message 
throw AppError.badRequest("Invalid request data");
throw AppError.conflict("Resource already exists");
throw AppError.internalServerError("Something went wrong");
throw AppError.notFound("Resource not found");
throw AppError.unauthorized("Access denied");
throw AppError.forbidden("Action forbidden");

5. trycatchWrapper Utility Functions

The trycatchWrapper family of functions simplifies error handling in different layers of the application (e.g., repository, service). They automatically catch errors and throw appropriate AppError instances.

Example Usage:

Standard Wrapper:

import { trycatchWrapper } from "backend-error-handler";

const fetchData = trycatchWrapper(async () => {
    // Your logic here
});

MongoDB-Specific Wrapper:

import { trycatchWrapperMongo } from "backend-error-handler";

const fetchMongoData = trycatchWrapperMongo(async () => {
    // Your MongoDB query here
});

Prisma-Specific Wrapper:

import { trycatchWrapperPrisma } from "backend-error-handler";

const fetchPrismaData = trycatchWrapperPrisma(async () => {
    // Your Prisma query here
});

Installation

Install the package via npm:

npm install backend-error-handler

Conclusion

By using the backend-error-handler package, you can:

  • Simplify error handling in controllers and services.
  • Ensure consistent error and success response formats.
  • Reduce boilerplate try-catch blocks.

For additional questions or support, please refer to the package documentation or reach out to me -> amirpoudel .