0.0.0 • Published 8 months ago

reasonable-error v0.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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

  //...
};
0.0.0

8 months ago

0.0.0-beta

9 months ago