Express : Call to next in error handler

非 Y 不嫁゛ 提交于 2020-08-26 07:14:49

问题


I am implementing a node + express js app, and I am having issues calling to the next function into the error handler.

I have a render middleware which is called by next in each each controler, and I would like it to be the same with my error handler. What I do in controler is putting some viewProperties in the reqand then call the next middleware which retrieve these properties and render the response consequently.

function render(req, res, next) {

  // viewName, title, args
  var properties = req.viewProperties || {};

  // We only handle a res.send(message)
  if (properties.body) {

    res.send(properties.body);
    return;
  }

  // We only handle a res.redirect(url)
  if (properties.redirect) {

    res.redirect(properties.redirect);
    return;
  }

  properties.lang = req.app.get('lang');
  properties.title = properties.title || 'Message_Me';
  properties.connected = req.session ? req.session.connected : false;
  properties.firstname = req.session.userFirstname || 'anonymous';

  res.render(properties.name, properties);
}

When I try using this middleware with my error handler, using next() the request is just pending on client side, and never recieved. So I try to create the same middleware as an error handler : The same function but with an arity of 4 and then call next(err) in my error handler. This time the response is revieved client side but it is not rendered properly, it only shows up the stack trace.

The only way I found, it to copy this function in my error handler and paste it instead of calling to next. I don't understand why it can't work properly ?

my error handler :

function redirectError(err, req, res, next) {

    // Ajax call running
    if (req.xhr) {

        req.viewProperties = { body : err.message };
        return next(err);
    }

    req.viewProperties = { name : 'layout/error', title : 'Erreur', message : err.message, err : err };

    // Here is the probleme
    next()
    // next(err);
}

EDIT

I tried another thing : I copied the render method into my error module as a simple function (not declared middleware). And then call to it instead of next in the redirectError error handler. that did the same behaviour. the function is called BUT nothing is recied on client side.

WHEREAS

If I copy the content of the render function INTO the redirectError everything works fine.

There really is something I don't understand here. It may be a deeper problem I have not yet noticed... Riddles In The Dark

EDIT N2

I figured out my mistake !! I forgot a return statement in a if of another middleware. That made the nextbeing called twice, and a very bad bahaviour...

As a conclusion, a good practice to adopt, is to always use return when calling next !

And thank's to laggingreflex which made me carry on.


回答1:


If there's an Error present (either thrown or passed through next) then only the next middleware which can handle the error (the one defined with arity of (err,req,res,next)) is called.

Conversely, if there isn't an Error present then the error handler middleware (err,req,res,next) is not called.

So in your case, your redirectError will only be called if there is an Error present, and your render only when there isn't.

To demonstrate:

app.use(function(req, res, next) {
    throw(new Error('testing...'));
});
app.use(function(req, res, next) {
    // This won't be called
});
app.use(function(err, req, res, next) {
    // But This would
    next(); // not passing any Error this time
});

app.use(function(err, req, res, next) {
    // So now this won’t be called
});
app.use(function(req, res, next) {
    // But this would
});


来源:https://stackoverflow.com/questions/27874418/express-call-to-next-in-error-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!