My suggestion would be to use domains. Careful use of domains should
obviate the need for try/catch altogether, and will handle async errors in
Express much better than the default error handler.

Getting Express / Connect up and running with domains is a little tricky,
but if you check out the NodeConf slides (
https://othiym23.github.io/nodeconf2013-domains/#/), they have some example
middleware functions that show you what's up.

Forrest

On Saturday, July 6, 2013, Tony Mobily wrote:

> Hi,
>
> I have been writing a bit of code with nodejs, and am sort of "going back"
> to brush things up, checking that I am doing things the right way.
> I recently read this:
>
> http://geoff.greer.fm/2012/06/10/nodejs-dealing-with-errors/
>
> After a bit of research, I got to this article:
>
> http://benno.id.au/blog/2011/08/08/nodejs-exceptions
>
> And to this SO answer:
>
>
> http://stackoverflow.com/questions/7310521/node-js-best-practice-exception-handling
>
> At the moment, I am only ever throwing() if:
>
> 1) I am enclosing _async_ code with try/catch, like this:
>
>  // Get the messages from Json, safely
>   try {
>     if( ! req.body.messages )
>       throw( new Error("req.body.messages not there") );
>     var messages = JSON.parse(req.body.messages);
>   } catch(e) {
>     var messages = [];
>   }
>
> 2) Something reeeeeeeeaaaaaaaalllllllyyyyyyy bad happens in terms of how
> my module was used. For example a class constructor is missing a necessary
> parameter, etc.
>
> In any other case, I am using next( err ). If something really bad
> happens, for example mongodb dies and calls to the db start failing, I
> handle it with an error manager in express:
>
> app.use(  function( err, req, res, next){
>  // ...
> });
>
> But... does this mean that if my application uses a library that has a
> random throw(), my app will effectively die?
> What's the "current" state of affairs?
>
> Looking at existing code, well, I seem to have gotten it right: nodejs
> libraries tend to only throw when things really aren't supposed to happen.
> For example in qs/lib/querystring.js:
>
> function stringifyString(str, prefix) {
>   if (!prefix) throw new TypeError('stringify expects an object');
>   return prefix + '=' + encodeURIComponent(str);
> }
>
> But... am I missing something?
> Would this be correct:
>
> * throw() when the program really deserves to die, and not for external
> causes (see: the db server goes down, etc.)
> * Always use next( err ) if anything goes wrong (business as usual)
> * Figure out if some libraries emit events, and listen to them if necessary
>
> Bye,
>
> Merc.
>
> --
> --
> 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 
> nodejs@googlegroups.com<javascript:_e({}, 'cvml', 'nodejs@googlegroups.com');>
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com <javascript:_e({}, 'cvml',
> 'nodejs%2bunsubscr...@googlegroups.com');>
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com <javascript:_e({}, 'cvml',
> 'nodejs%2bunsubscr...@googlegroups.com');>.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to