Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle exception with domain module in restify framework? #659

Closed
ioubo opened this issue Aug 6, 2014 · 6 comments
Closed

How to handle exception with domain module in restify framework? #659

ioubo opened this issue Aug 6, 2014 · 6 comments
Labels

Comments

@ioubo
Copy link

ioubo commented Aug 6, 2014

How to get the uncatched exception in restify framework? I try to use server.on('uncaughtException', but it cannot work.

@ioubo ioubo changed the title How to get the uncatched exception in restify framework? How to handle exception with domain module in restify framework? Aug 6, 2014
@epappas
Copy link

epappas commented Aug 7, 2014

Hi ioubo,

Can you share us a code snippet please?
It works for me, I had run into troubles before thought, as I's causing an exception inside the Handler, so the handler was failing silently.

Cheers.

@ioubo
Copy link
Author

ioubo commented Aug 9, 2014

The following is my code snippet:

var server = restify.createServer( {
name: 'AppService'
})
server.listen( config.port, config.service_ip, function () {
console.log( '%s listening at %s ', server.name, server.url );
})

var errorhandler = domain.create();
errorhandler.on('error', function (e) {
console.log(e);
});

errorhandler.run(function () {
var appservice = require('./routes/appservice');

server.get({ path: config.prefix_route + '/getClientSetting' }, appservice.getClientSetting);
server.get({ path: config.prefix_route + '/getBatchFutures' }, appservice.getBatchFutures);
server.get({ path: config.prefix_route + '/getFutureTrend' }, appservice.getFutureTrend);
server.get({ path: config.prefix_route + '/getFutureDetail' }, appservice.getFutureDetail);
server.get({ path: config.prefix_route + '/getCandleStick' }, appservice.getCandleStick);

});

@epappas
Copy link

epappas commented Aug 10, 2014

If you want to create your own domain, I would suggest wrapping your domain as a middleware, and crete one for each request, just make sure to use it hight enough. But doing that you replicate what restify does already for you.

This is my example of rectify server, handling exceptions:

var server = restify.createServer({
    name: '....',
    log: logger
});

Using the middlewares of your preference:

server.use( ... middleware ... );
server.use( ... middleware ... );
server.use( ... middleware ... );

example of routing

server.get(ROUTE_HERE, controller.getContent);

As an error handler:

server.on('uncaughtException', function (req, res, route, err) {
    // logging here, maybe?
    res.send(err.code || 500, {
        code: err.code || 500,
        error_description: err.status || err.message || err.description || 'Internal Server Error',
        req_body: req.params
    });
});

Finally:

server.on('after', function (req, res, route, err) { ... last logger here, if logger was no set during server creation ... });

@ioubo
Copy link
Author

ioubo commented Aug 10, 2014

Thanks for your reply. As your way, how to get the stack trace of the error?

@epappas
Copy link

epappas commented Aug 10, 2014

If the err is an instance of Error, you can easily log the err.stack.
Otherwise, I don't think you can even print the stack in your custom domains.

console.log(err.stack);

@micahr
Copy link
Member

micahr commented Jun 29, 2015

Looks like this issue was answered by epappas. If server.on('uncaughtException') is still not working for you, please open a new ticket with some code examples and the errors that aren't being emitted.

@micahr micahr closed this as completed Jun 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants