The output from the two files above is shown below. Notice how it contains no references to errs.js
:
## Merging with Existing Errors
When working with errors you catch or are returned in a callback you can extend those errors with properties by using the errs.merge
method. This will also create a human readable error message and stack-trace:
process.on('uncaughtException', function(err) {
console.log(errs.merge(err, {namespace: 'uncaughtException'}));
});
var file = fs.createReadStream('FileDoesNotExist.here');
{ [Error: Unspecified error]
name: 'Error',
namespace: 'uncaughtException',
errno: 34,
code: 'ENOENT',
path: 'FileDoesNotExist.here',
description: 'ENOENT, no such file or directory \'FileDoesNotExist.here\'',
stacktrace: [ 'Error: ENOENT, no such file or directory \'FileDoesNotExist.here\'' ] }
## Optional Callback Invocation
Node.js handles asynchronous IO through the elegant EventEmitter
API. In many scenarios the callback
may be optional because you are returning an EventEmitter
for piping or other event multiplexing. This complicates code with a lot of boilerplate:
function importantFeature(callback) {
return someAsyncFn(function (err) {
if (err) {
if (callback) {
return callback(err);
}
throw err;
}
});
}
errs
it presents a common API for both emitting error
events and invoking continuations (i.e. callbacks) with errors. If a callback
is supplied to errs.handle()
it will be invoked with the error. It no callback
is provided then an EventEmitter
is returned which emits an error
event on the next tick:
function importantFeature(callback) {
return someAsyncFn(function (err) {
if (err) {
return errs.handle(err, callback);
}
});
}
## Piping Errors
Often when working with streams (especially when buffering for whatever reason), you may have already returned an EventEmitter
or Stream
instance by the time an error is handled.
function pipeSomething(callback) {
var stream = new require('stream').Stream;
getAnotherStream(function (err, source) {
if (err) {
if (callback)
callback(err);
}
stream.emit('error', err);
return;
}
source.pipe(stream);
})
return stream;
}
You may pass either a function
or EventEmitter
instance to errs.handle
.
function pipeSomething(callback) {
var stream = new require('stream').Stream;
getAnotherStream(function (err, source) {
if (err) {
return errs.handle(err, callback || stream);
}
source.pipe(stream);
})
return stream;
}
If you wish to invoke both a callback
function and an error
event simply pass both:
errs.handle(err, callback, stream);
Methods
The errs
modules exposes some simple utility methods:
.create(type, opts)
: Creates a new error instance for with the specified type
and opts
. If the type
is not registered then a new Error
instance will be created..register(type, proto)
: Registers the specified proto
to type
for future calls to errors.create(type, opts)
..unregister(type)
: Unregisters the specified type
for future calls to errors.create(type, opts)
..handle(err, callback)
: Attempts to instantiate the given error
. If the error
is already a properly formed error
object (with a stack
property) it will not be modified..merge(err, type, opts)
: Merges an existing error with a new error instance for with the specified type
and opts
.
Installation
Installing npm (node package manager)
$ curl http://npmjs.org/install.sh | sh
Installing errs
$ [sudo] npm install errs
Tests