trycatch
An asynchronous domain-based try/catch exception handler with (optional) long stack traces for node.js
With the update to 0.2.0
- error-handling is now domain-based
- long-stack-traces are optional (off by default)
- No nasty Error.prepareStackTrace hack (and no corresponding memory leaks)
Also, trycatch conforms to try/catch V8 best practices.
Install
npm install trycatch
Use
var trycatch = require('trycatch')
trycatch(fnTry, fnCatch)
Optional Long-Stack-Traces:
var trycatch = require('trycatch')
trycatch.configure({'long-stack-traces': true})
trycatch(fnTry, fnCatch)
Colors:
var trycatch = require('trycatch')
trycatch.configure({
colors: {
'node': 'none',
'node_modules': false,
'default': 'yellow'
}
})
trycatch(fnTry, fnCatch)
Advanced Formatting:
var trycatch = require('trycatch')
trycatch.configure({
format: function(line) {
return line
}
})
trycatch(fnTry, fnCatch)
Basic Example
var trycatch = require("trycatch"),
_ = require('underscore')._
trycatch(function() {
_.map(['Error 1', 'Error 2'], function foo(v) {
setTimeout(function() {
throw new Error(v)
}, 10)
})
}, function(err) {
console.log("Async error caught!\n", err.stack);
});
Output
Advanced Examples
See the /test
and examples
directories for more use cases.
Returning 500s on Server Request
http.createServer(function(req, res) {
trycatch(function() {
setTimeout(function() {
throw new Error('Baloney!');
}, 1000);
}, function(err) {
res.writeHead(500);
res.end(err.stack);
});
}).listen(8000);
Visit http://localhost:8000 and get your 500.