verror / 1.6.1 last updated 6 months ago created on Apr 18th 2012
Install
npm install --save verror
Clone
git clone git@github.com:davepacheco/node-verror.git
dependencies
main |
|
core-util-is | 1.0.2 | MIT |
|
extsprintf | 1.2.0 | MIT |
maintainers
versions
17 total
1.6.1 | 6 months ago | dap |
1.6.0 | 2 years ago | dap |
1.5.1 | 2 years ago | dap |
1.5.0 | 2 years ago | dap |
1.4.0 | 2 years ago | dap |
1.3.7 | 3 years ago | dap |
1.3.6 | 3 years ago | dap |
1.3.5 | 4 years ago | dap |
1.3.4 | 4 years ago | anonymous |
1.3.3 | 4 years ago | anonymous |
1.3.2 | 4 years ago | anonymous |
1.3.1 | 4 years ago | anonymous |
1.3.0 | 4 years ago | anonymous |
1.2.0 | 4 years ago | dap |
1.1.0 | 4 years ago | dap |
1.0.1 | 4 years ago | dap |
1.0.0 | 4 years ago | dap |
readme
verror: richer JavaScript errors
This module provides two classes:
- VError, for combining errors while preserving each one's error message, and
- WError, for wrapping errors.
Both support printf-style error messages using extsprintf.
printf-style Error constructor
At the most basic level, VError is just like JavaScript's Error class, but with printf-style arguments:
This prints:
err.stack
works the same as for built-in errors:
This prints:
Causes
You can also pass a cause
argument, which is another Error. For example:
This prints out:
which resembles how Unix programs typically report errors:
To match the Unixy feel, just prepend the program's name to the VError's
message
.
You can also get the next-level Error using err.cause()
:
prints:
Of course, you can nest these as many times as you want:
This prints:
The idea is that each layer in the stack annotates the error with a description of what it was doing (with a printf-like format string) and the result is a message that explains what happened at every level.
WError: wrap layered errors
Sometimes you don't want an Error's "message" field to include the details of
all of the low-level errors, but you still want to be able to get at them
programmatically. For example, in an HTTP server, you probably don't want to
spew all of the low-level errors back to the client, but you do want to include
them in the audit log entry for the request. In that case, you can use a
WError, which is created exactly like VError (and also supports both
printf-style arguments and an optional cause), but the resulting "message" only
contains the top-level error. It's also more verbose, including the class
associated with each error in the cause chain. Using the same example above,
but replacing err3
's VError with WError, so that it looks like this:
we get this output:
That's what we wanted -- just a high-level summary for the client. But we can get the object's toString() for the full details:
For a complete example, see examples/werror.js.
Contributing
Contributions welcome. Code should be "make check" clean. To run "make check", you'll need these tools:
If you're changing something non-trivial or user-facing, you may want to submit an issue first.