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;console; /*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;var error = ep;error;console; /*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 = ; console; var n = epnext ;ep;console;console;console; /*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 = ;ep;ep;
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;console; /*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;console;console;console;
If vars
is provided, the variables found in the error properties that are String will be set with the given value.
ep;console; /*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;ep;ep;