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

@whi/http-errors

v0.2.0

Published

HTTP error classes with Response conversion utilities

Readme

@whi/http-errors

npm version

TypeScript HTTP error classes with Web Response API conversion utilities for modern JavaScript runtimes.

Installation

npm install @whi/http-errors

Usage

HttpError

Base error class for HTTP-like errors with built-in Response conversion.

import { HttpError } from '@whi/http-errors';

// Create an HTTP error
const error = new HttpError(404, 'Resource not found');

// Convert to Response object
const response = error.toResponse();
// Response: { status: 404, body: { "error": "Resource not found" } }

// With additional details
const detailedError = new HttpError(500, 'Database error', {
    code: 'DB_CONNECTION_FAILED',
    retry: true
});

const response2 = detailedError.toResponse();
// Response body: { "error": "Database error", "code": "DB_CONNECTION_FAILED", "retry": true }

// With custom response headers
const authError = new HttpError(401, 'Session expired', null, {
    'Set-Cookie': 'session_id=; HttpOnly; Secure; SameSite=Strict; Max-Age=0; Path=/'
});

const response3 = authError.toResponse();
// Response includes Set-Cookie header to clear the session

MissingFieldError

Specialized error for missing required fields (returns 400 Bad Request).

import { MissingFieldError } from '@whi/http-errors';

const error = new MissingFieldError('email');
// Status: 400
// Message: "Missing required field: email"
// Details: { field: "email" }

Creating HttpError from Response

Convert error responses back into HttpError objects.

import { HttpError } from '@whi/http-errors';

// From JSON error response
const response = await fetch('/api/endpoint');
if (!response.ok) {
    const error = await HttpError.fromResponse(response);
    console.log(error.status);   // 404
    console.log(error.message);  // "Not found"
    console.log(error.details);  // { ... }
}

API

HttpError

Constructor:

new HttpError(status: number, message: string, details?: Record<string, any>, headers?: Record<string, string>)

Properties:

  • status: number - HTTP status code
  • message: string - Error message
  • details?: Record<string, any> - Additional error details
  • headers?: Record<string, string> - Custom response headers

Methods:

  • toResponse(): Response - Converts error to a JSON Response object
  • static fromResponse(response: Response): Promise<HttpError> - Creates HttpError from a Response

MissingFieldError

Constructor:

new MissingFieldError(fieldName: string, headers?: Record<string, string>)

Extends HttpError with status 400 and pre-formatted message.

Use Cases

Perfect for:

  • Cloudflare Workers - Throw and catch errors that convert to proper HTTP responses
  • Edge Functions - Deno, Bun, or any runtime with Web APIs
  • API Routes - Standardized error handling for fetch-based applications
  • Service Workers - Consistent error responses in PWAs

License

LGPL-3.0

Author

Matthew Brisebois

Repository

github.com/webheroesinc/js-http-errors