Contextualizer
Easily wraps errors passed to callbacks to provide more context.
Installation
$ npm install contextualizer
The Problem
When you have an error passed up through various levels of callbacks, you can't tell which function called the lower level function.
The Solution
You need to wrap the error. Contextualizer is a thin wrapper on verror (recommended here: https://www.joyent.com/developers/node/design/errors) that allows you to write this:
var VError = ; { ;}
Like this:
var addContext = { ;}
Usage
contextualizer(
error
[, message]
)
error
(Error|Function) - The error to wrap. In the event of no error (falsy value), that same falsy value will be returned. Alternatively, you can pass the entire callback function in here and it will be wrapped. See not recommended but better than nothing below.message
(string) - An optional message to prepend to error message of the wrapped error. If nothing is passed, the default of[error wrapper]
will be used. If the first argument is a function, an error will be thrown if there is no message passed.
Examples
Here's an example of how you might use it in an express app.
The first 2 endpoints don't use contextualizer, the second do.
Note that I'm assigning the "contextualizer" function to addContext
var addContext = var express = var request = // This is a function that's called in a lot of different places in our app.// It'll send an error to the callback if one happens. { process} // Now here's a fake express app that will collect data from various placesvar app = ; /*** These endpoints have bad logging ***/ var badLogRouter = express // Save data from the website badLogRouter // Save data from the API badLogRouter app /*** These endpoints have good logging ***/ var goodLogRouter = express // Save data from the website goodLogRouter // Save data from the API goodLogRouter app /*** Here's the error middleware where the errors get logged ***/app
Errors returned from the first 2 endpoints look exactly the same in the logs and don't contain any context that can be helpful for troubleshooting.
Error: worst database ever at /Users/nigel/aboutme/contextualizer/scriptjs:10:18 at process
The errors returned from /GoodLog/web/save
have the endpoint in the stack trace
VError: [error wrapper]: worst database ever
at /Users/nigel/about.me/contextualizer/script.js:41:34
at /Users/nigel/about.me/contextualizer/script.js:10:9
at process._tickCallback (node.js:355:11)
And the errors from /GoodLog/api/save
take it a step further and have a custom
error message prepended to the passed error message
VError: error saving from API in good log router: worst database ever
at /Users/nigel/about.me/contextualizer/script.js:49:34
at /Users/nigel/about.me/contextualizer/script.js:10:9
at process._tickCallback (node.js:355:11)
Not recommended but better than nothing
You can get lazy and pass the whole callback, not just the error, to contextualizer like this:
// fake library function { process} // the function in which we want to add context to errors { ;}
This is convenient but instead of handleResponse
being in the stack trace, code within contextualizer will be the top frame. The line where you wrapped the callback is nowhere to be found. However, the error message will be prepended with your message. Like this:
VError: error in dataRequest: this database is horrible
at contextualizerWrapper (/Users/nigel/about.me/contextualizer/index.js:21:28)
at databaseInnards (/Users/nigel/about.me/contextualizer/script.js:73:9)
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3