Fix handling of Errors in Chrome

This commit is contained in:
Peter Wagenet
2012-11-02 10:39:49 -07:00
parent a927a9b0ad
commit 39832c0633
2 changed files with 25 additions and 9 deletions
+6 -4
View File
@@ -1,14 +1,16 @@
var Handlebars = require("./base");
// BEGIN(BROWSER)
var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
Handlebars.Exception = function(message) {
var tmp = Error.prototype.constructor.apply(this, arguments);
for (var p in tmp) {
if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
// Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
for (var idx = 0; idx < errorProps.length; idx++) {
this[errorProps[idx]] = tmp[errorProps[idx]];
}
this.message = tmp.message;
};
Handlebars.Exception.prototype = new Error();
+19 -5
View File
@@ -61,13 +61,27 @@ function compileWithPartials(string, hashOrArray, partials) {
}
function shouldThrow(fn, exception, message) {
var caught = false;
var caught = false,
exType, exMessage;
if (exception instanceof Array) {
exType = exception[0];
exMessage = exception[1];
} else if (typeof exception === 'string') {
exType = Error;
exMessage = exception;
} else {
exType = exception;
}
try {
fn();
}
catch (e) {
if (e instanceof exception) {
caught = true;
if (e instanceof exType) {
if (!exMessage || e.message === exMessage) {
caught = true;
}
}
}
@@ -480,14 +494,14 @@ test("rendering undefined partial throws an exception", function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{> whatever}}");
template();
}, Handlebars.Exception, "Should throw exception");
}, [Handlebars.Exception, 'The partial whatever could not be found'], "Should throw exception");
});
test("rendering template partial in vm mode throws an exception", function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{> whatever}}");
template();
}, Handlebars.Exception, "Should throw exception");
}, [Handlebars.Exception, 'The partial whatever could not be found'], "Should throw exception");
});
test("rendering function partial in vm mode", function() {