Socket
Book a DemoInstallSign in
Socket

@juliancoleman/named-error

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@juliancoleman/named-error

A tiny library for creating custom errors

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Named Error

A tiny library for creating custom errors for use with Bluebird's filtered catch

Example

import NamedError from "@juliancoleman/named-error";

// Instantiate with `new`
const NotFoundError = new NamedError(404, "Not found.");

This library is also particularly useful with APIs that use an ORM, such as ObjectionJS.

import Base from "./base";

import NamedError from "@juliancoleman/named-error";

export class UserNotFoundError extends NamedError { }

export default class User extends Base {
  static tableName = "users";
  static NotFoundError = new UserNotFoundError(404, "User not found.");

  // ...
}

And when we need to catch

import Service from "../services/some-service";
import NotFoundError from "../errors/not-found-error";

Service.getThatOneThing() // Returns a Bluebird promise
  .then(doSomethingWithReturnedData)
  .catch(TypeError, NotFoundError, function(e) {
    // Generic catch-all for unexpected errors
  });

FAQs

Package last updated on 15 Mar 2019

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