New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

sendable-error

Package Overview
Dependencies
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sendable-error

Composable errors to simplify creating useful failure responses for APIs

latest
Source
npmnpm
Version
0.10.0
Version published
Weekly downloads
213
2.9%
Maintainers
1
Weekly downloads
 
Created
Source

sendable-error · GitHub license npm version

Composable errors to simplify creating useful failure responses for APIs

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
import { SendableError } from "sendable-error";

try {
  throw new SendableError({
    status: 400,
    code: "validation/missing-required",
    message: "Missing required field 'id'",
    public: true,
    details: {
      field: "id"
    }
  })
} catch (error) {
  return SendableError.of(error).toResponse();
}

Response with status code 400:

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

Getting Started

npm i sendable-error

Throwing Errors

Creating a new error from scratch:

 throw new SendableError({
  code: CODE_MISSING_REQUIRED,
  message: "Missing required field 'id'",
  public: true,
  details: {
    field: "id",
  },
});

Or provide a cause for the error:

throw new SendableError({
  code: CODE_DATABASE_ERROR,
  cause: error,
});

Or even transform an error from elsewhere into a SendableError:

throw SendableError.of(error, {
  code: CODE_DATABASE_ERROR
});

Sending Errors

Express

app.use((error, req, res, next) => {
  SendableError.of(error).send(res);
});

WinterTC Compatible

export const handler = async (request: Request) => {
  try {
    // do something that might throw
  } catch (error) {
    return SendableError.of(error).toResponse();
  }
}

Others

try {
  // do something that might throw
} catch (error) {
  const responseBody = SendableError.of(error).toResponseBody();
  /* send responseBody */
}

Keywords

typescript

FAQs

Package last updated on 02 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts