reasonable-error
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

Reasonable Error

Create type-safe custom error classes with a specific reason + associated context.

import { createErrorClass } from "reasonable-error";

type ReasonContextMap = {
  ValidationError: {
    paths: Array<string>;
  };
  UnknownError: undefined;
  NotAuthorized: {
    missingRoles: Array<string>;
  };
};

const { CreateUserError, isCreateUserError } =
  createErrorClass<ReasonContextMap>()("CreateUserError");

const createUser = () => {
  //...

  throw new CreateUserError("Not authorized to create user!", {
    reason: "NotAuthorized",
    context: {
      missingRoles: ["user:write"],
    },
  });

  //...
};

Package Sidebar

Install

npm i reasonable-error

Weekly Downloads

3

Version

0.0.0

License

MIT

Unpacked Size

33.4 kB

Total Files

12

Last publish

Collaborators

  • henriqueinonhe