More than a couple people mentioned try/catch not working in node.js in the
Fibers 0.5 thread (
https://groups.google.com/forum/?fromgroups#!topic/nodejs/5hv6uIBpDl8), so
I thought I'd offer a friendly reminder of the trycatch module that handles
these situations nicely.

FWIW, try/catch doesn't have to be broken in node.js and it doesn't have to
lost state any more than you're willing to let it:

https://npmjs.org/package/trycatch

trycatch catches all uncaughtExceptions with long stack traces and can be
nested. We use trycatch in production at RedRobot (http://redrobot.com/),
adding a great deal of stability and proper error handling (500s). We also
use it in conjunction with our control-flow library stepdown which
incorporates it at every "step" if it's installed.

(The readme's out of date, so checkout the passing tests)
https://npmjs.org/package/stepdown

The following example catches and properly passes back all errors generated
both by it, passed to its callbacks, or uncaughtExceptions in core or 3rd
party module code complete with long stack traces, allowing you to fail
gracefully,  ignore the error, or let it bubble.

var $$ = require('stepdown')

function foo(arg, callback) {
  $$([
    function($) {
      asyncOne(arg, $.first())
      asyncTwo(arg, $.first())
    }
  , function($, res) {
      var onNestedComplete = $.first()

      // Nesting
      $$([
          function($) {
            setTimeout(function() {
              // Async error
              // Will bubble to nestedCallback
              throw new Error('will be caught')
            })
          }
        , function ($) {
            // not called
          }
        ]
      , function nestedCallback(err) {
          // ignore above error
          err = err && err.message === 'will be caught' ? null : err
          onNestedComplete(err)
        })
    }
  // Pass unhandled exceptions and callback errors
  ], callback)
}


Cheers,
Adam Crabtree

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to