Skip to content

Repository files navigation

chubbyts-http-error

CI Coverage Status Mutation testing badge npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

Creates http error which can be catched and converted to error responses.

Requirements

  • node: 22

Installation

Through NPM as @chubbyts/chubbyts-http-error.

npm i @chubbyts/chubbyts-http-error@^3.5.0

Usage

import { createNotFound } from '@chubbyts/chubbyts-http-error/dist/http-error';

try {
  throw createNotFound({ detail: 'Something went wrong', instance: 'server-1', otherKey: 'otherValue' });
} catch (e) {
  console.log(e);
}

Data

  • detail, instance: problem details (RFC 9457)
  • headers: the headers the error middleware should set on the error response (e.g. www-authenticate, allow, retry-after), never part of the body
  • any other key: additional problem details, keys with an undefined value get skipped, reserved keys (type, status, title, ...) get ignored

Error middleware

import { isHttpError } from '@chubbyts/chubbyts-http-error/dist/http-error';

const errorMiddleware: Middleware = async (request, handler) => {
  try {
    return await handler(request);
  } catch (e) {
    if (isHttpError(e)) {
      const { headers, _httpError, ...body } = e;

      return new Response(JSON.stringify(body), {
        status: e.status,
        headers: { 'content-type': 'application/problem+json', ...headers },
      });
    }

    throw e;
  }
};

Copyright

2026 Dominik Zogg

About

Creates http error which can be catched and converted to error responses.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages