restify-router icon indicating copy to clipboard operation
restify-router copied to clipboard

Error handler is always executed

Open henryjw opened this issue 5 years ago • 4 comments

Does this library support adding error-handling middleware via router.use()? I'm trying to create a generic handler for a router, but it's being executed before the route handlers.

Example:

app.get('/api/data', function dataHandler(req, res, next) {
    // Process request
});


// Always executes before `dataHandler()`. 
app.use(function errorHandler(err, req, res, next) {
    // Handle error
});

henryjw avatar Dec 31 '18 17:12 henryjw

Hi @henryjw , handlers registered via the .use method will always run before route handlers. The router interface just proxies the behaviour of the restify server. The .use method on the server is meant to add handlers which execute before route handlers. From what I understand, you wish to add a post route handler?

ukayani avatar Dec 31 '18 18:12 ukayani

Thanks for the quick response @ukayani. Yup, a post route handler is basically what I'm looking for. Is there a way to implement that with this library?

henryjw avatar Dec 31 '18 18:12 henryjw

At the moment there is no such feature. It should not be too hard to add it though. I'll try to take a look over the weekend.

ukayani avatar Jan 03 '19 15:01 ukayani

Thanks!

henryjw avatar Jan 07 '19 12:01 henryjw