Socket
Book a DemoInstallSign in
Socket

erratum

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

erratum

A simple Error factory with support for string formatting and additional data

Source
npmnpm
Version
1.1.1
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

Erratum

Erratum is a simple extension of the native Error class supporting string formatting, additional properties and further subclassing.

Breaking changes

Erratum has been rewritten with the 1.x release, extending the native Error class to better support error specialization.

While the basic erratum() API remains unchanged, versions 1.x do not have the erratum.wrap feature anymore.

API

The API is comprised of only one function: Erratum([data], message, [args...]). It can be used both as a factory function and as a constructor and always returns an Erratum instance.

  • data: [optional] additional data
  • message: the error message, optionally with utils.format() formatting tags
  • args: [optional] formatting arguments

Here's a quick example:

var Erratum = require('erratum');

var err = Erratum({statusCode: 500}, 'The %s is: %s', 'answer', 42);
// var err = new Erratum({statusCode: 500}, 'The %s is: %s', 'answer', 42);

err instanceof Error                   // true
err instanceof Erratum                 // true
err.statusCode === 500                 // true
err.message === 'This answer is: 42';  // true

The Erratum function supports extension through child classes. These will inherit all Erratum features.

function ExtendedErratum() {
  Erratum.apply(this, arguments);
  // Custom stuff goes here.
}

util.inherits(ExtendedErratum, Erratum);

var err = new ExtendedErratum({statusCode: 500}, 'The answer is: %s', 42);
    
err instanceof Error                   // true
err instanceof Erratum                 // true
err instanceof ExtendedErratum         // true
err.statusCode === 500                 // true
err.message === 'This answer is: 42';  // true

Check the tests in test/main.js for more examples.

Error wrapping

If data is an instance of Error or has either the err property or the error property set to an instance of Error, the stack of the new error returned by Erratum() will incorporate the stack of the provided error. The latter will be available at Erratum#wrapperError.

> var someError = new Error('Something went wrong.');
> throw new Erratum({err: someError}, 'Yes, something definitely went wrong.');

Erratum: Yes, something definitely went wrong.
    at repl:1:7
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)
Caused By: Error: Something went wrong.
    at repl:1:17
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)

Test

$ mocha

License

See LICENSE.

FAQs

Package last updated on 13 Sep 2016

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