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"],
    },
  });

  //...
};

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.00latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.00
0.0.0-beta0

Package Sidebar

Install

npm i reasonable-error

Weekly Downloads

0

Version

0.0.0

License

MIT

Unpacked Size

33.4 kB

Total Files

12

Last publish

Collaborators

  • henriqueinonhe