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

@web-ts-toolkit/http-errors

v0.34.3

Published

Typed HTTP error classes and payload helpers for backend APIs

Readme

@web-ts-toolkit/http-errors

Typed HTTP error classes and structured error payload helpers for backend APIs.

Installation

pnpm add @web-ts-toolkit/http-errors

Highlights

  • typed 4xx and 5xx error classes
  • HttpError, ClientError, and ServerError base classes
  • machine-readable error metadata
  • helpers for AIP-193 and RFC 9457 payloads

Constructor Status Contract

HttpError accepts only finite integer HTTP error status codes from 400 through 599. ClientError narrows that to 400 through 499, and ServerError narrows it to 500 through 599. Invalid or category-mismatched statuses throw synchronously before an error instance is created.

Quick Start

import {
  HttpError,
  ServiceUnavailableError,
  UnauthorizedError,
  toAip193ErrorPayload,
  toRfc9457ErrorPayload,
  toRfc9457ValidationErrorPayload,
} from '@web-ts-toolkit/http-errors';

throw new UnauthorizedError();
throw new UnauthorizedError('missing bearer token');

throw new HttpError(503);
throw new HttpError(503, 'please try again later');

throw new ServiceUnavailableError();

const error = new HttpError(400, 'Email must be a valid address.', {
  reason: 'INVALID_EMAIL',
  domain: 'api.example.com',
  type: 'https://api.example.com/problems/invalid-email',
  title: 'Invalid email address',
  instance: '/problems/invalid-email/123',
  metadata: { field: 'email' },
  errors: [{ detail: 'must be a valid email address', pointer: '#/email' }],
});

const aip193 = toAip193ErrorPayload(error);
const rfc9457 = toRfc9457ErrorPayload(error);
const validation = toRfc9457ValidationErrorPayload(error);
void [aip193, rfc9457, validation];

Main Exports

  • HttpError
  • ClientError and ServerError
  • specific error classes such as BadRequestError, ForbiddenError, NotFoundError
  • toAip193ErrorPayload(...)
  • toRfc9457ErrorPayload(...)
  • toRfc9457ValidationErrorPayload(...)
  • types including HttpErrorOptions, HttpErrorShape, HttpErrorProblemFields, HttpErrorMetadataValue, Aip193ErrorInfoDetail, Aip193ErrorPayload, Rfc9457ErrorPayload, and Rfc9457ValidationError

AIP-193 Serializer Contract

toAip193ErrorPayload(...) returns a Google-style { error } envelope. It emits code, status, message, and details on every payload. The first detail is always an error_info entry whose reason defaults to the error reason or canonical status, whose domain defaults to the error domain or the serializer fallback domain, and whose metadata is copied when present.

import { BadRequestError, toAip193ErrorPayload } from '@web-ts-toolkit/http-errors';

const payload = toAip193ErrorPayload(
  new BadRequestError('Email must be a valid address.', {
    reason: 'INVALID_EMAIL',
    domain: 'api.example.com',
    metadata: { field: 'email' },
  }),
);

// payload.error.details[0] is:
// { type: 'error_info', reason: 'INVALID_EMAIL', domain: 'api.example.com', metadata: { field: 'email' } }
void payload;

RFC 9457 Serializer Contract

toRfc9457ErrorPayload(...) emits required RFC 9457 problem members (type, title, status, and detail) on every payload. If the input shape has errors typed as an array, the returned payload preserves that entry type for custom extension errors.

When type is missing it falls back to about:blank. When title is missing it falls back to the canonical HTTP status title, or Unknown for unmapped status codes. instance is emitted only when present.

import { BadRequestError, toRfc9457ErrorPayload } from '@web-ts-toolkit/http-errors';

const payload = toRfc9457ErrorPayload(
  new BadRequestError('Email must be a valid address.', {
    type: 'https://api.example.com/problems/invalid-email',
    title: 'Invalid email address',
    instance: '/problems/invalid-email/123',
    errors: [{ detail: 'must be a valid email address', pointer: '#/email' }],
  }),
);

// payload is:
// { type, title, status: 400, detail: 'Email must be a valid address.', instance, errors }
void payload;

toRfc9457ValidationErrorPayload(...) is the validation-specific helper. It accepts JavaScript input defensively and only emits errors entries that have an own string detail plus optional own string pointer, parameter, and header fields. Non-arrays, empty arrays, inherited validation fields, and invalid entries are omitted from the returned errors array; if no valid entries remain, errors is omitted.

import { BadRequestError, toRfc9457ValidationErrorPayload } from '@web-ts-toolkit/http-errors';

const payload = toRfc9457ValidationErrorPayload(
  new BadRequestError('Email must be a valid address.', {
    errors: [{ detail: 'must be a valid email address', pointer: '#/email', debug: 'omitted' }, { pointer: '#/name' }],
  }),
);

// payload.errors is [{ detail: 'must be a valid email address', pointer: '#/email' }]
void payload;

External Payload Disclosure

Serializers emit public payload fields verbatim. Treat message, details, errors, metadata, type, title, and instance as externally visible API response data. Do not put secrets, stack traces, raw upstream errors, tokens, credentials, or internal diagnostics in these fields unless you intentionally want clients to receive them.

Structured Value Ownership

HttpError snapshots supported top-level collections at construction. metadata is normalized into an error-owned frozen string record, and array-valued details and errors are copied into error-owned frozen arrays. Mutating the source objects after construction cannot add, remove, reorder, or rename error-owned top-level entries.

Serializers also return fresh top-level arrays and metadata records, so mutating one payload does not change the source error or later payloads. This is a shallow boundary: nested detail and error entry objects remain shared, and ErrorOptions.cause identity is preserved.

Documentation

Full package documentation lives in website/docs/packages/http-errors.md.

  • live docs: https://web-ts-toolkit.pages.dev/docs/packages/http-errors