feat($interpolate): provide contextual error messages

if an exception occurs during interpolation of a string
(e.g. name() in "Hello, {{name()}}!" throws an exception) we now print
an error message with the expression that was being evaluated when the
exception was thrown.
This commit is contained in:
Brian Ford 2012-07-19 14:07:00 -07:00 committed by Igor Minar
parent d3fa7a2e9e
commit d804bbcd51
3 changed files with 40 additions and 11 deletions

View file

@ -52,7 +52,7 @@ function $InterpolateProvider() {
}; };
this.$get = ['$parse', function($parse) { this.$get = ['$parse', '$exceptionHandler', function($parse, $exceptionHandler) {
var startSymbolLength = startSymbol.length, var startSymbolLength = startSymbol.length,
endSymbolLength = endSymbol.length; endSymbolLength = endSymbol.length;
@ -124,18 +124,24 @@ function $InterpolateProvider() {
if (!mustHaveExpression || hasInterpolation) { if (!mustHaveExpression || hasInterpolation) {
concat.length = length; concat.length = length;
fn = function(context) { fn = function(context) {
for(var i = 0, ii = length, part; i<ii; i++) { try {
if (typeof (part = parts[i]) == 'function') { for(var i = 0, ii = length, part; i<ii; i++) {
part = part(context); if (typeof (part = parts[i]) == 'function') {
if (part == null || part == undefined) { part = part(context);
part = ''; if (part == null || part == undefined) {
} else if (typeof part != 'string') { part = '';
part = toJson(part); } else if (typeof part != 'string') {
part = toJson(part);
}
} }
concat[i] = part;
} }
concat[i] = part; return concat.join('');
}
catch(err) {
var newErr = new Error('Error while interpolating: ' + text + '\n' + err.toString());
$exceptionHandler(newErr);
} }
return concat.join('');
}; };
fn.exp = text; fn.exp = text;
fn.parts = parts; fn.parts = parts;

View file

@ -175,7 +175,7 @@ describe('Binder', function() {
$rootScope.error['throw'] = function() {throw 'MyError';}; $rootScope.error['throw'] = function() {throw 'MyError';};
errorLogs.length = 0; errorLogs.length = 0;
$rootScope.$apply(); $rootScope.$apply();
expect(errorLogs.shift()).toBe('MyError'); expect(errorLogs.shift().message).toBe('Error while interpolating: {{error.throw()}}\nMyError');
$rootScope.error['throw'] = function() {return 'ok';}; $rootScope.error['throw'] = function() {return 'ok';};
$rootScope.$apply(); $rootScope.$apply();

View file

@ -25,6 +25,29 @@ describe('$interpolate', function() {
expect($interpolate('{{ false }}')()).toEqual('false'); expect($interpolate('{{ false }}')()).toEqual('false');
})); }));
it('should rethrow exceptions', inject(function($interpolate, $rootScope) {
$rootScope.err = function () {
throw new Error('oops');
};
expect(function () {
$interpolate('{{err()}}')($rootScope);
}).toThrow('Error while interpolating: {{err()}}\nError: oops');
}));
it('should stop interpolation when encountering an exception', inject(function($interpolate, $compile, $rootScope) {
$rootScope.err = function () {
throw new Error('oops');
};
var dom = jqLite('<div>{{1 + 1}}</div><div>{{err()}}</div><div>{{1 + 2}}</div>');
$compile(dom)($rootScope);
expect(function () {
$rootScope.$apply();
}).toThrow('Error while interpolating: {{err()}}\nError: oops');
expect(dom[0].innerHTML).toEqual('2');
expect(dom[1].innerHTML).toEqual('{{err()}}');
expect(dom[2].innerHTML).toEqual('{{1 + 2}}');
}));
it('should return interpolation function', inject(function($interpolate, $rootScope) { it('should return interpolation function', inject(function($interpolate, $rootScope) {
$rootScope.name = 'Misko'; $rootScope.name = 'Misko';