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 🙏

© 2025 – Pkg Stats / Ryan Hefner

error-response-handler

v1.0.5

Published

Comprehensive error handling and response formatting for Node.js applications

Downloads

24

Readme

Error Response Handler

npm version License: MIT CI/CD TypeScript

A comprehensive error handling and response formatting solution for Express.js applications with full TypeScript support.

Author: Bapi Majumder
Email: [email protected]
GitHub: bapimajumder

✨ Features

  • Built-in HTTP status codes with auto-completion
  • Automatic async error handling
  • Consistent response structures
  • Custom error classes with status codes
  • TypeScript & JavaScript support
  • Framework agnostic (Express, Koa, etc.)
  • Production-ready error handling

📦 Installation

npm install error-response-handler
# or
yarn add error-response-handler
# or
pnpm add error-response-handler


🚀 Quick Start
Basic Usage
typescript
import { 
  successResponse, 
  errorResponse, 
  AppError, 
  StatusCode,
  asyncHandler
} from 'error-response-handler';

// Success response
const success = successResponse({
  data: { id: 1, name: 'John' },
  message: 'User fetched',
  statusCode: StatusCode.OK
});

// Error response
const error = errorResponse({
  message: 'Validation failed',
  statusCode: StatusCode.BAD_REQUEST,
  error: { email: 'Invalid format' }
});

// Custom error
throw new AppError('Not found', StatusCode.NOT_FOUND);

// Async handler
const fetchUser = asyncHandler(async (id) => {
  const user = await getUser(id);
  if (!user) throw new AppError('User not found', 404);
  return user;
});
🛠️ Express.js Integration
typescript
import express from 'express';
import { asyncHandler, AppError, successResponse } from 'error-response-handler';

const app = express();

// Success route
app.get('/success', (req, res) => {
  res.json(successResponse({ data: { healthy: true } }));
});

// Protected route
app.get('/users/:id', asyncHandler(async (req, res) => {
  const user = await getUserById(req.params.id);
  if (!user) {
    throw new AppError('User not found', StatusCode.NOT_FOUND);
  }
  res.json(successResponse({ data: user }));
}));

// Error middleware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  const statusCode = err instanceof AppError ? err.statusCode : 500;
  res.status(statusCode).json(errorResponse({
    message: err.message,
    statusCode,
    error: process.env.NODE_ENV === 'development' ? err.stack : undefined
  }));
});

app.listen(3000);
📚 API Reference
successResponse(options)
Creates a standardized success response.

Options:

Parameter	Type	Description	Default
message	string	Custom message	Status text
data	any	Response payload	undefined
statusCode	number/string/StatusCodeValue	HTTP status code	200
errorResponse(options)
Creates a standardized error response.

Options:

Parameter	Type	Description	Default
message	string	Error message	Status text
error	any	Error details	undefined
statusCode	number/string/StatusCodeValue	HTTP status code	500
asyncHandler(fn)
Wraps async functions to automatically catch errors.

AppError
Custom error class extending Error with status code support.

typescript
new AppError(message: string, statusCode: StatusInput, isOperational?: boolean)
🌐 Status Codes
All standard HTTP status codes available:

Code	Constant	Text
200	SUCCESS	Success
201	CREATED	Created
204	NO_CONTENT	No Content
400	BAD_REQUEST	Bad Request
401	UNAUTHORIZED	Unauthorized
403	FORBIDDEN	Forbidden
404	NOT_FOUND	Not Found
500	INTERNAL_SERVER_ERROR	Internal Server Error
Full list available in statusCodes.ts


📜 License
Distributed under the MIT License. See LICENSE for more information.

📬 Contact
Bapi Majumder  - [email protected]

Project Link: https://github.com/BapiMajumder1402/error-response-handler