Node-restify: custom error handler

Created on 30 Dec 2012  路  5Comments  路  Source: restify/node-restify

I can user some code like

  app.use(function(err, req, res, next) {
    ...
  });

in express to translate the error information.

How about in restify?

Most helpful comment

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

doesn't work .

C:\tfs.mercer.com\ICS.DevStack\mobile-api\controllers\blurb.controller.js:17
throw new Error('The message');// error("new");
^

Error: The message

I am using restify 6.0.1. Does any changes in error handling?

All 5 comments

In restify, you're "encouraged" to use subclasses of HttpError/RestError; see the error handling section in the docs: http://mcavage.github.com/node-restify/#Error-handling

To serialize onto the wire, you need to handle them in formatters by instanceof, etc. For example, here's the default JSON one: https://github.com/mcavage/node-restify/blob/master/lib/formatters/json.js#L7-L28

You need to do that for whatever content-types you want to support via res.send(err) or next(err).

Related: you can override "uncaughtErrors" in restify with this:

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

The default is to log and kick a 500.

Lastly, I know you didn't really ask, but this is one of those places where restify is really divergent from express; I kind of hate that syntax where the thing above is "like a handler, but not". So you handle errors like any other data type in a formatter.

Thank you for you reply.I hate the blurry handler as the middleware too. I inherited errors from your RestError, but I want to return the errors in my format, customized formatter is a better approach.

btw, I do not open a new issue since I am not sure it's a bug or not.
please see https://github.com/mcavage/node-restify/blob/master/lib/errors/rest_error.js#L80

Maybe it should be opts.constructorOpt = cause.constructorOpt; ?

Yep it is - pushed, and thanks!

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

doesn't work .

C:\tfs.mercer.com\ICS.DevStack\mobile-api\controllers\blurb.controller.js:17
throw new Error('The message');// error("new");
^

Error: The message

I am using restify 6.0.1. Does any changes in error handling?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blechatellier picture blechatellier  路  4Comments

stevehipwell picture stevehipwell  路  8Comments

domenic picture domenic  路  4Comments

anuxs picture anuxs  路  3Comments

0v3rst33r picture 0v3rst33r  路  6Comments