This package has been deprecated

Author message:

deprecated

error-provider

0.0.6 • Public • Published

error-provider

Node.js project

Manages errors for third-party modules

Version: 0.0.6

When you're writing a third-party module you'll probably deal with errors. If you want to send custom errors to your users you should send a code and then in your documentation explain these codes.

This module eases the error management, you only need to give a number, a code and a description and when you need them just get and throw them.

The built-in errors are also available so if you need to manually throw a EEXIST you can do so.

The error description accepts variables, that is, you can create an error with a variable and when you get it you can set the variable value:

ep.create (1000, "MY_CUSTOM_ERROR", "This is a {which} error");
console.log (ep.get ("MY_CUSTOM_ERROR", { which: "custom" }));
 
/*
Prints:
 
{ [MY_CUSTOM_ERROR: This is a custom error] errno: 1000, code: "MY_CUSTOM_ERROR" }
*/

If you want to add another property different from errno, code and description you can do it, just pass an object with the new properties (if the property value is a string it can also have variables):

ep.create (ep.next (), "MY_CUSTOM_ERROR", "This is a {which} error", {
    path: "{path}",
    fn: function (){
        console.log ("custom error");
    }
});
var error = ep.get ("MY_CUSTOM_ERROR", { which: "custom", path: "some/path" });
error.fn ();
console.log (error);
 
/*
Prints:
 
custom error
{ [MY_CUSTOM_ERROR: This is a custom error]
  errno: 100,
  code: "MY_CUSTOM_ERROR",
  path: "some/path",
  fn: [Function] }
*/

Installation

npm install error-provider

Example

var ep = require ("error-provider");
 
console.log (ep.get (ep.ENOENT));
 
var n = ep.next ();
ep.create (n, "TEST1", "test 1");
console.log (ep.get (ep.TEST1));
console.log (ep.get ("TEST1"));
console.log (ep.get (n));
 
/*
Prints:
 
{ [ENOENT: no such file or directory] errno: 34, code: "ENOENT" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
*/

Methods

There's a special method called local() that returns a local error provider. If you need a local storage with custom errors that you don't need to make public, local() can help you. If another third-party module uses the error-provider module you won't see its errors, the local error provider is local to your module. The first available errno is 0 and the set of default codes is empty, that is, you can't return a built-in error like EEXIST. You can create all the storages you want.

var ep = require ("error-provider").local ();
ep.create (ep.next (), ...);
ep.get (...);

ep.create(errno, code, description[, properties])
Creates an error with an id, code, description and additional properties.

The strings can contain variables that can be set later. A variable is a name encapsulated inside curly braces:

ep.create (ep.next (), "TEST1", "test 1", {
    path: "{p}"
});
console.log (ep.get ("TEST1", { p: "some/path" }));
 
/*
Prints:
 
{ [TEST1: test 1] errno: 100, code: "TEST1", path: "some/path" }
*/

ep.get(id[, vars])
Returns an error. It can be a Node.js built-in error such as ENOENT or one that was created previously.

The id can be a Number, String or Object:

ep.create (ep.next (), "TEST1", "Test 1");
console.log (ep.get (ep.TEST1));
console.log (ep.get ("TEST1"));
console.log (ep.get (100));

If vars is provided, the variables found in the error properties that are String will be set with the given value.

ep.create (ep.next (), "TEST1", "test {number}", {
    n: "{number}",
    p: "{value}"
});
console.log (ep.get ("TEST1", { number: 1, value: "ok" }));
 
/*
Prints:
 
{ [TEST1: test 1]
  errno: 100,
  code: "TEST1",
  n: "1",
  p: "ok" }
*/

ep.next()
Returns the next available error number and increments the counter. This number is an identifier to create errors.

The first available number starts at 100.

The following example creates three errors with identified with 100, 101 and 102.

ep.create (ep.next (), ...);
ep.create (ep.next (), ...);
ep.create (ep.next (), ...);

Readme

Keywords

none

Package Sidebar

Install

npm i error-provider

Weekly Downloads

222

Version

0.0.6

License

none

Last publish

Collaborators

  • gagle