errs / 0.3.2 last updated a year ago created on Feb 24th 2012
Install
npm install --save errs
Clone
git clone git@github.com:indexzero/errs.git
dependencies
No dependencies listed.
maintainers
versions
8 total
| 0.3.2 | a year ago | indexzero |
| 0.3.1 | a year ago | indexzero |
| 0.3.0 | 2 years ago | indexzero |
| 0.2.4 | 3 years ago | indexzero |
| 0.2.3 | 4 years ago | indexzero |
| 0.2.2 | 4 years ago | indexzero |
| 0.2.0 | 4 years ago | indexzero |
| 0.1.1 | 4 years ago | indexzero |
readme
errs
Simple error creation and passing utilities focused on:
- Creating Errors
- Reusing Error Types
- Merging with Existing Errors
- Optional Callback Invocation
- Piping Error Events
Creating Errors
You should know by now that a String is not an Error. Unfortunately the Error constructor in Javascript isn't all that convenient either. How often do you find yourself in this situation?
Rest your fingers, errs is here to help. The following is equivalent to the above:
Reusing Custom Error Types
errs also exposes an inversion of control interface for easily reusing custom error types across your application. Custom Error Types registered with errs will transparently invoke Error constructor and Error.captureStackTrace to attach transparent stack traces:
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:
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:
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:
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.
You may pass either a function or EventEmitter instance to errs.handle.
If you wish to invoke both a callback function and an error event simply pass both:
Methods
The errs modules exposes some simple utility methods:
.create(type, opts): Creates a new error instance for with the specifiedtypeandopts. If thetypeis not registered then a newErrorinstance will be created..register(type, proto): Registers the specifiedprotototypefor future calls toerrors.create(type, opts)..unregister(type): Unregisters the specifiedtypefor future calls toerrors.create(type, opts)..handle(err, callback): Attempts to instantiate the givenerror. If theerroris already a properly formederrorobject (with astackproperty) it will not be modified..merge(err, type, opts): Merges an existing error with a new error instance for with the specifiedtypeandopts.
Installation
Installing npm (node package manager)
Installing errs
Tests
All tests are written with vows and should be run with npm: