error-provider

Manages errors for third-party modules

error-provider

Node.js project

Version: 0.0.2

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:
 
{ [Error: MY_CUSTOM_ERROR]
  errno: 1000,
  code: "MY_CUSTOM_ERROR",
  description: "This is a 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}",
    fnfunction (){
        console.log ("custom error");
    }
});
var error = ep.get ("MY_CUSTOM_ERROR", { which: "custom", path: "some/path" });
error.fn ();
console.log (error);
 
/*
Prints:
 
custom error
{ [Error: MY_CUSTOM_ERROR]
  errno: 100,
  code: "MY_CUSTOM_ERROR",
  description: "This is a custom error",
  path: "some/path",
  fn: [Function] }
*/
npm install error-provider
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:
 
{ [Error: ENOENT]
  errno: 34,
  code: "ENOENT",
  description: "no such file or directory" }
{ [Error: TEST1] errno: 100, code: "TEST1", description: "test 1" }
{ [Error: TEST1] errno: 100, code: "TEST1", description: "test 1" }
{ [Error: TEST1] errno: 100, code: "TEST1", description: "test 1" }
*/

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:
 
{ [Error: TEST1]
  errno: 100,
  code: "TEST1",
  description: "Test 1",
  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:
 
{ [Error: TEST1]
  errno: 100,
  code: "TEST1",
  description: "Test 1",
  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 (), ...);