An asynchronous domain-based exception handler with long stack traces for node.js
JavaScript Shell
Latest commit 28e6184 Jan 6, 2016 @CrabDude 1.5.21
Failed to load latest commit information.
lib Don't overwrite existing prepareStackTrace (e.g., node-source-map-sup… Jan 6, 2016
test Fix shallow stack traces when trycatch() isn't used Sep 1, 2015
.travis.yml Fix Travis versions Dec 11, 2015
LICENSE Remove Tim Caswell (wrong project) Mar 27, 2012
package.json 1.5.21 Jan 6, 2016 Bump hookit to fix breaking bug Aug 31, 2015
trycatch.old.js Add support for strict mode Sep 1, 2015


Build Status A domain-based asynchronous try/catch with (optional) long stack traces for node.js optimized for V8.

WARNING: trycatch replaces the built-in global Error object.


npm install trycatch


var trycatch = require('trycatch')
trycatch(function() {
  // do something error-prone
}, function(err) {

###Returning 500s on Server Request

http.createServer(function(req, res) {
  trycatch(function() {
    setTimeout(function() {
      throw new Error('Baloney!');
    }, 1000);
  }, function(err) {

Visit http://localhost:8000 and get your 500.

#Options Optional Long-Stack-Traces:

// Because trycatch shims all native I/O calls,
// it must be required & configured with 'long-stack-traces' before any other modules.
var trycatch = require('trycatch')
trycatch.configure({'long-stack-traces': true})
trycatch(fnTry, fnCatch)


var trycatch = require('trycatch')
  colors: {
    // 'none' or falsy values will omit
    'node': 'none',
    'node_modules': false,
    'default': 'yellow'
trycatch(fnTry, fnCatch)

Advanced Formatting:

var trycatch = require('trycatch')
  format: function(line) {
    // Alter final output (falsy values will omit)
    return line
trycatch(fnTry, fnCatch)

#Basic Example

var trycatch = require("trycatch"),
  _ = require('underscore')._

  trycatch(function() {['Error 1', 'Error 2'], function foo(v) {
    setTimeout(function() {
      throw new Error(v)
    }, 10)
}, function(err) {
  console.log("Async error caught!\n", err.stack);


#Advanced Examples

See the /test and examples directories for more use cases.


trycatch effectively wraps all application callbacks in try/catch blocks, preventing an exception in your application code from causing code in core to not execute. Effectively, this means that excpeitons originating in application code that normally would be passed as uncaughtException, can instead be handled via uncaughtApplicationException without requiring a restart:

process.on('uncaughtApplicationException', (err) => console.log(err.stack))

process.on('uncaughtException', (err) => {
  // We are in an undefined state and need to restart