Skip to content

Commit

Permalink
Fix handling of Errors in Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Nov 2, 2012
1 parent a927a9b commit 39832c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
10 changes: 6 additions & 4 deletions lib/handlebars/utils.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
24 changes: 19 additions & 5 deletions spec/qunit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 39832c0

Please sign in to comment.