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

@naman_deep_singh/errors

v2.3.4

Published

Standardized error classes and Express middleware for consistent error handling with TypeScript

Downloads

1,335

Readme

@naman_deep_singh/errors

**Version:** 2.3.4

A standardized, code-driven error handling system for TypeScript and Express applications, providing consistent error identity, responses, and middleware integration.

🚀 Features

✅ Structured Error Classes — AppError, HTTPError, ValidationError, etc.
✅ Strongly-Typed Error Codes — Centralized error identity via constants
✅ Centralized Error Messages — One source of truth for user-facing messages
✅ Express Middleware — Error converter & global handler
✅ Response Integration — Works seamlessly with @naman_deep_singh/http-response
✅ TypeScript First — Full type safety & IntelliSense
✅ Consistent API Responses — Unified error shape across services
✅ Extendable Error Messages — Add or override messages at runtime

📦 Installation
npm install @naman_deep_singh/errors

🧠 Design Philosophy

This package is code-driven, not message-driven.

Errors are identified by stable error codes.

Messages are resolved internally via centralized mappings.

API contracts remain stable even if messages change.

Benefits:

✅ Consistency across microservices
✅ Safe refactoring
✅ Better logging, tracing, and observability

🔧 Usage
Creating Errors (Recommended)
import {
  BadRequestError,
  UnauthorizedError,
  NotFoundError,
  ValidationError,
  InternalServerError,
} from '@naman_deep_singh/errors'

// Basic usage
throw new BadRequestError()

throw new UnauthorizedError()

throw new NotFoundError()

// With additional details
throw new ValidationError({
  fields: ['email', 'password'],
})

throw new InternalServerError()

🧾 Error Codes & Messages
import {
  ERROR_CODES,
  ERROR_MESSAGES,
  ErrorCode,
} from '@naman_deep_singh/errors'


ERROR_CODES — Canonical list of all supported error codes

ERROR_MESSAGES — Mapping of error codes → user-facing messages

ErrorCode — Union type of all valid error codes

Why Error Codes?

✅ Consistent error identity across services
✅ Centralized message management
✅ Safer API contracts
✅ Improved logging & observability

🌐 Express Middleware
import express from 'express'
import {
  errorConverter,
  expressErrorHandler,
  ValidationError,
} from '@naman_deep_singh/errors'

const app = express()

// Convert unknown / third-party errors → AppError
app.use(errorConverter)

// Handle all errors consistently
app.use(expressErrorHandler)

app.post('/users', (req, res) => {
  if (!req.body.email) {
    throw new ValidationError({
      fields: ['email'],
    })
  }
})


Middleware Responsibilities

errorConverter — Converts unknown errors into AppError, preserves operational errors

expressErrorHandler — Sends standardized API responses and integrates with @naman_deep_singh/http-response

🔗 Integration
With @naman_deep_singh/http-response
import { responderMiddleware } from '@naman_deep_singh/http-response'
import { expressErrorHandler } from '@naman_deep_singh/errors'

app.use(responderMiddleware())
app.use(expressErrorHandler)

With @naman_deep_singh/server
import { createServer } from '@naman_deep_singh/server'
import { expressErrorHandler } from '@naman_deep_singh/errors'

const server = createServer('My API', '1.0.0')
server.app.use(expressErrorHandler)

🧠 Extending & Adding Error Messages

You can safely extend existing errors or add new codes/messages dynamically.

Extending Existing Error Class
import { InternalServerError, ERROR_CODES } from '@naman_deep_singh/errors'

export class CryptoIntegrityError extends InternalServerError {
  constructor(details?: unknown, cause?: Error) {
    super(ERROR_CODES.CRYPTO_INTEGRITY_ERROR, details, cause)
  }
}

Registering Custom Error Messages
import { errorMessageRegistry } from '@naman_deep_singh/errors'

// Add new messages or override existing ones
errorMessageRegistry.register({
  CUSTOM_ERROR: 'Something went wrong with custom logic',
  VALIDATION_FAILED: 'Custom validation failed message',
})


After this, AppError or any derived class will use the updated messages automatically.

import { AppError, ErrorCode } from '@naman_deep_singh/errors'

throw new AppError(
  'CUSTOM_ERROR' as ErrorCode,
  500,
  {
    reason: "Something went wrong with custom logic"
  }
)

📚 Available Error Classes
Class	Status Code	Use Case
AppError	Custom	Base error class
HTTPError	4xx / 5xx	Base HTTP error
BadRequestError	400	Invalid input
UnauthorizedError	401	Authentication failures
TokenExpiredError	401	Expired tokens
TokenMalformedError	401	Invalid token format
ForbiddenError	403	Authorization failures
NotFoundError	404	Resource not found
ConflictError	409	Resource conflicts
ValidationError	422	Validation errors
RateLimitError	429	Rate limiting
CryptoIntegrityError	500	Crypto validation failure
InternalServerError	500	Server-side failures
ServiceUnavailableError 500 Service not available

🎯 Standard Error Response

All errors resolve to a consistent response shape:

{
  "success": false,
  "code": "VALIDATION_FAILED",
  "message": "Validation failed",
  "details": {
    "fields": ["email"]
  },
}

📄 License

ISC © Naman Deep Singh