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

Who should handle errors in THE callback? #98

Closed
defeo opened this issue Mar 14, 2013 · 3 comments
Closed

Who should handle errors in THE callback? #98

defeo opened this issue Mar 14, 2013 · 3 comments

Comments

@defeo
Copy link

defeo commented Mar 14, 2013

Hi,

Most engines in consolidate do error handling using a code similar to this one (example taken from the Eco wrapper):

try {
  fn(null, engine.render(str, options));
} catch (err) {
  fn(err);
}

I'm concerned about what happens when an exception is raised inside the callback fn, but outside of the engine.render call. In my opinion, fn should be responsible for handling errors happening inside engine.render, but not for errors happening inside fn itself.

Consider an example where the code of fn contains an undefined reference. In this case an exception is raised inside the first call to fn, caught, then eventually raised again inside the second call to fn. This is not necessarily the behavior one would expect. For a concrete example, consider the following express application

var express = require('express');
var cons = require('consolidate');

var called = false

express()
  .engine('js', cons.hogan)
  .set('views', __dirname)
  .get('*', function(req, res) {
    res.render('demo.js', function(err, result) {
      if (called) throw new Error('Already called');
        called = true;
        ress.send(result);    // ress is undefined
      });
  }).listen(8080);

Here the real problem is that the variable ress is undefined, but the user receives a Error('Already called') instead. This example is inspired by a real world issue I had trying to use async.js together with consolidate.js.

I'm relatively new to express, but IMHO it would make more sense to rewrite the engine wrappers along the following lines:

try {
  var result = engine.render(str, options);
} catch (err) {
  fn(err);
  return;
}
fn(null, result);
@tj
Copy link
Owner

tj commented Mar 14, 2013

yeah we shouldn't invoking the callback twice, this is a common bug with node crap

@ForbesLindesay
Copy link
Contributor

This was the thinking behind #85 which fixes this issue.

@doowb
Copy link
Collaborator

doowb commented Sep 17, 2017

Closing this but working on getting the changes merged in from the mentioned PR.

@doowb doowb closed this as completed Sep 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants