Restify and error handling via domains

804 views
Skip to first unread message

Mil Werns

unread,
May 4, 2013, 7:59:55 AM5/4/13
to res...@googlegroups.com
Hi,

while looking into the restify source code, I discovered that the restify function lib/server.js => Server.prototype._run() creates a domain for a request.
After testing I realised that the domain prevents the server from crashing when an uncaught exception occurs. This also applies for exceptions in callbacks.

1.) Can you please summarize the current state of restfiy and domains?
2.) Is it really true that I can throw an exception anywhere in the code (e.g. in a deep nested callback) and the request specific domain will catch it?
3.) Is it possible to set the statusCode for a domain-catched exception? I tried this:


  setTimeout(     // test error in callback
      function() {
          console.log("timeout reached");
          // throw error for testing domains
          var err = new Error("my error");
          err.statusCode = 404;
          throw err;
          // shouldn't be called
          return next();
        },
        100
      );


Restify catchs the error and the result is the following JSON object:

{
    "code": "InternalError",
    "message": "my error"
}


but the statusCode is 500 (not 404).

Thank you
Mil
Message has been deleted

Mark Cavage

unread,
May 9, 2013, 11:40:01 AM5/9/13
to res...@googlegroups.com
Hi Mil,

Sorry - yes, restify creates a domain per request so that any and all uncaught exceptions are caught and bubbled up.  Note that 3rd-party libraries that do their own domain management will stomp this (so things like redis/mongo for example need to be overridden with req.domain instead of the one they provide).  To catch uncaught exceptions:

server.on('uncaughtException', function (req, res, route, err) {
   ...
});

There's a default handler that gets run if you don't listen for that event, which just returns a 500.

m
On Thu, May 9, 2013 at 8:30 AM, Mil Werns <milw...@gmail.com> wrote:
Anyone?
--
You received this message because you are subscribed to the Google Groups "restify" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restify+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mil Werns

unread,
May 10, 2013, 5:58:19 AM5/10/13
to res...@googlegroups.com
Thank you Mark. Do you see any problems to use server.on('uncaughtException',...) as the catcher for already handled errors?

E.g.:

A callback in the application:

try {
  // an exception occured
} catch(err) {
  // handle the error because I read the warning "Don't Ignore Errors!" at http://nodejs.org/api/domain.html
  var error = new Error("my message");
  error.statusCode = 404;
  throw error;
}

The server.js:


server.on('uncaughtException', function (req, res, route, err) {
  // extend the error and log a lot of debgging informations
  res.send(err.statusCode, err.message);
});

My goal is to extend every error with debugging messages and log this error before sending the response.

Or is there a better way?
I asume that I can write a own formatter but than I have to reimplement the application/json formatter functionality...
Reply all
Reply to author
Forward
0 new messages