Skip to content

Simple abstraction to handle custom errors in your codebase.

Notifications You must be signed in to change notification settings

segment-boneyard/errors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

errors

Simple abstraction to handle custom errors in your codebase.

Build Status

Installation

$npm install segmentio/errors
$component install segmentio/errors

Example

var RandomError = require('./random-error');
var errors = require('errors')();


/**
 * Add `Random` error to our known errors map.
 */

errors.add('Random', RandomError);


/**
 * Wrap any errors the map recognizes after getting from Mongo.
 */

function get (id, callback) {
  mongo.findById(id, function (err, res) {
    callback(errors.wrap(err), res);
  });
}
// random-error.js

var inherit = require('util').inherits;


/**
 * Random Error.
 */

function RandomError (err) {
  Error.call(this);
  Error.captureStackTrace(this, arguments.callee);
}

inherit(RandomError, Error);


/**
 * Check if `err` is a `RandomError`.
 */

RandomError.is = function (err) {
  return err && err.code == 1042;
};

API

errors()

Initialize a new error map.

.Errors

The map of error constructors, so you can expose it so external APIs can do things like check instanceof.

#add(name, Constructor)

Add a new error Constructor to the map with name. The Constructor must have a function to check whether the error is its type exposed as .is.

#match(error)

Check with the error matches any of the rules of the custom constructors.

#wrap(error)

Wrap an error in its appropriate custom constructor.

About

Simple abstraction to handle custom errors in your codebase.

Resources

Stars

Watchers

Forks

Packages

No packages published