Skip to content

Commit 084e8fe

Browse files
committed
Provide better error on unexpected template
Fixes #806
1 parent 9f07a34 commit 084e8fe

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/handlebars/runtime.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export function template(templateSpec, env) {
2727
if (!env) {
2828
throw new Exception("No environment passed to template");
2929
}
30+
if (!templateSpec || !templateSpec.main) {
31+
throw new Exception('Unknown template object: ' + typeof templateSpec);
32+
}
3033

3134
// Note: Using env.VM references rather than local var references throughout this section to allow
3235
// for external users to override these as psuedo-supported APIs.
@@ -151,6 +154,7 @@ export function template(templateSpec, env) {
151154
throw new Exception('_child can not be used with depthed methods');
152155
}
153156

157+
// TODO : Fix this
154158
return container.programWithDepth(i);
155159
};
156160
return ret;

spec/runtime.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*globals Handlebars, shouldThrow */
2+
3+
describe('runtime', function() {
4+
describe('#template', function() {
5+
it('should throw on invalid templates', function() {
6+
shouldThrow(function() {
7+
Handlebars.template({});
8+
}, Error, 'Unknown template object: object');
9+
shouldThrow(function() {
10+
Handlebars.template();
11+
}, Error, 'Unknown template object: undefined');
12+
shouldThrow(function() {
13+
Handlebars.template('');
14+
}, Error, 'Unknown template object: string');
15+
});
16+
it('should throw on version mismatch', function() {
17+
shouldThrow(function() {
18+
Handlebars.template({
19+
main: true,
20+
compiler: [Handlebars.COMPILER_REVISION + 1]
21+
});
22+
}, Error, /Template was precompiled with a newer version of Handlebars than the current runtime/);
23+
shouldThrow(function() {
24+
Handlebars.template({
25+
main: true,
26+
compiler: [Handlebars.COMPILER_REVISION - 1]
27+
});
28+
}, Error, /Template was precompiled with an older version of Handlebars than the current runtime/);
29+
shouldThrow(function() {
30+
Handlebars.template({
31+
main: true
32+
});
33+
}, Error, /Template was precompiled with an older version of Handlebars than the current runtime/);
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)