Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@smallwins/err

Package Overview
Dependencies
Maintainers
6
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smallwins/err

Slightly better custom Error

  • 2.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
6
Weekly downloads
 
Created
Source

@smallwins/err

Slightly better custom Errors

  • Same runtime interface: just pass a message to the constructor
  • Respects extends with clean name and stack properties
  • Adds toObject returns a plain object representation
  • Bundles common HTTP error types with a code property
npm i @smallwins/err --save

Bundled Error Types

  • err.Err a base Error type intended for extending
  • err.InternalError has a code property of 500
  • err.DatabaseError has a code property of 500
  • err.NotFoundError has a code property of 404
  • err.NotAuthorizedError has a code property of 403

Usage

Example usage:

var err = require('@smallwins/err')

let notFound = new err.NotFoundError('missing record')
console.log(err.code) // logs 404

Subclass to add additional properties such as code:

var err = require('@smallwins/err')

class CoffeeError extends err.Err {
  constructor(params) {
    super(params)
    this.code = 500
  }
}

let e = new CoffeeError('lactose intolerant')
console.log(e.code) // logs 500

Get a clean representation:

console.log(e.toObject())
// logs {name, code, message, stack}

Extend by requireing error directly:

var Err = require('@smallwins/err/err')

class TerribleError extends Err {
  constructor(msg) {
    super(msg)
    this.extra = 'extra info'
  }
}

var e = new TerribleError('wut')
console.log(e.extra)

Work oldschool without new:

var err = require('@smallwins/err/oldschool')

console.log(err.Err('basic') instanceof Error)
// logs true

console.log(err.NotFound('not found err').toString())
// logs NotFound: not found err

@smallwins/err/oldschool API

Factory functions which return real Error instances:

  • err.Err returns an Err instance
  • err.Internal returns an InternalError instance
  • err.Database returns a DatabaseError instance
  • err.NotFound returns a NotFoundError instance
  • err.NotAuthorized returns a NotAuthorizedError instance

Runtime type checking fully supported. Check the tests.

Keywords

FAQs

Package last updated on 29 Aug 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc