Handle synchronous exceptions better#85
Handle synchronous exceptions better#85ForbesLindesay wants to merge 1 commit intotj:masterfrom ForbesLindesay:patch-2
Conversation
There was a problem hiding this comment.
What does finished do? Thanks.
There was a problem hiding this comment.
It prevents the callback being called multiple times. For example:
cons.temp.render = function (str, opts, cb) {
try {
cb(new Error('this never goes well');
} catch (ex) {
cb(ex);
}
};
cons.temp.render('', {}, function (err, res) {
console.log('There was an error');
throw err;
});In there the console.log will be hit twice:
There was an error
There was an error
Error: this never goes well
Stack Trace
Stack Trace
Stack Trace
Stack Trace
This kind of bug exists in reality in lots of places in the code and it's really hard to debug, so it's easiest to just fix it at the top level.
There was a problem hiding this comment.
Can we just
try {
var result = engine.render(str, options);
} catch (err) {
return fn(err);
}
fn(null, result);There was a problem hiding this comment.
You can for synchronous parsers, and that's always sufficient for the synchronous parsers, but it does nothing to prevent poorly implemented async templating libraries.
There was a problem hiding this comment.
I don't even know how it's possible to make a synchronous parser in node .. and that's why I'm curious why most of the engines don't have a callback for it's compile/render functions.
There was a problem hiding this comment.
The only thing that would force you to make your template parsing async is if you allow users to do async work inside their templates. Node.js can do pretty much anything synchronously if you want it to (making http web requests takes a bit of work). Most of the libraries just transform strings to functions so there's no real reason for there to be any async work there.
P.S. The only two that actually operate asyncronously are dust and qejs the rest just trick you into thinking they do by providing a node style apie when in fact their syncronous (for an example take a look at the source code for the render function for jade no Async there P.S. it also nicely demonstrates a template library that suffers from the problem finished would fix.)
|
I know this is old, and with the promise support that has been added, it might not be an issue. If it is, then I think we can add this logic to the |
Same as #83 but with a fixed diff