@mtth/stl-errors
TypeScript icon, indicating that this package has built-in type declarations

0.8.2 • Public • Published

Standard errors

Motivation

Good error handling is a prerequisite for good telemetry. To help with this, @mtth/stl-errors provides a simple StandardError interface which exposes powerful building blocks:

  • Namespaced error codes;
  • Causal chains;
  • Optional structured data.

Quickstart

Standard errors are best created via errorFactories which provides type-safe error creation functions along with their codes:

import {errorFactories} from '@mtth/stl-errors';

const [errors, codes] = errorFactories({
  definitions: {
    invalidFoo: (foo: string) => ({
      message: `The input foo ${foo} was invalid`,
      tags: {foo},
    }),
    missingBar: 'The bar was missing',
  },
});

// Error with code `ERR_INVALID_FOO` (`codes.InvalidFoo`).
const err1 = errors.invalidFoo('fff');

// Error with code `ERR_MISSING_BAR` (`codes.MissingBar`).
const err2 = errors.missingBar();

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @mtth/stl-errors

    Weekly Downloads

    47

    Version

    0.8.2

    License

    MIT

    Unpacked Size

    53.7 kB

    Total Files

    18

    Last publish

    Collaborators

    • mtth