最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Express : Call to next in error handler - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 12, 2015 at 19:14 Jérémie Briand asked Jan 10, 2015 at 8:48 Jérémie BriandJérémie Briand 131 silver badge4 bronze badges 2
  • Is redirectError the last middleware? If so there's nothing to goto next to, so not sure why you're calling that. – laggingreflex Commented Jan 10, 2015 at 13:24
  • the last middleware is render. It is set after redirectError so there is indeed a next to go to. I even tested it with logs and the render middleware print it correctly – Jérémie Briand Commented Jan 11, 2015 at 10:31
Add a ment  | 

1 Answer 1

Reset to default 6

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
});
发布评论

评论列表(0)

  1. 暂无评论