Skip to content

Composable errors to simplify creating useful failure responses for APIs

License

Notifications You must be signed in to change notification settings

Censkh/sendable-error

Repository files navigation

Composable errors to simplify creating useful failure responses for APIs

npm i sendable-error

Note: this package is in early development, use with caution

SendableErrors provide built-in support for:

  • An easy to use builder interface to construct errors
  • A unified way to send your errors as a JSON response
  • Error codes to easily identify error types on the client side
  • Public and private messages & details so your APIs don't leak technical information yet retaining verbose logging
  • Trace IDs allow you to identify specific errors and allows user's to point you in the right direction when they encounter a bug
  • A customizable logger interface

Getting Started

Creating a new error from scratch:

import {SendableError, ErrorCode} from "sendable-error";

const CODE_MISSING_REQUIRED = new ErrorCode({id: "validation/missing-required", defaultMessage: "Missing required field");

export const updateUser = (req, res) => {
    if (!req.body.id) {
        return new SendableError({
          code: CODE_MISSING_REQUIRED,
          message: "Missing required field 'id'",
          details: {
            field: "id"
          }
        })
          .send(res);
    }
};

In this example if you miss the ID field from the body you shall receive a 400 status code error with the body:

{
  "code": "validation/missing-required",
  "message": "Missing required field 'id'",
  "traceId": "8ab9c56a-90d1-5e71-b67a-d6b725837802",
  "details": {
    "field": "id"
  }
}

About

Composable errors to simplify creating useful failure responses for APIs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published