From c5b9ceb982a1242effd596ecf69be77c12c91850 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Thu, 19 Jul 2012 14:07:00 -0700 Subject: [PATCH 1/3] naive implementation for better error messages from --- src/ng/interpolate.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ng/interpolate.js b/src/ng/interpolate.js index 820ec4659249..b1c17d99aab9 100644 --- a/src/ng/interpolate.js +++ b/src/ng/interpolate.js @@ -98,20 +98,25 @@ function $InterpolateProvider() { exp, concat = []; - while(index < length) { - if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) && - ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { - (index != startIndex) && parts.push(text.substring(index, startIndex)); - parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex))); - fn.exp = exp; - index = endIndex + endSymbolLength; - hasInterpolation = true; - } else { - // we did not find anything, so we have to add the remainder to the parts array - (index != length) && parts.push(text.substring(index)); - index = length; + try { + while(index < length) { + if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) && + ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { + (index != startIndex) && parts.push(text.substring(index, startIndex)); + parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex))); + fn.exp = exp; + index = endIndex + endSymbolLength; + hasInterpolation = true; + } else { + // we did not find anything, so we have to add the remainder to the parts array + (index != length) && parts.push(text.substring(index)); + index = length; + } } } + catch (err) { + throw new Error('Error within interpolation: ' + text + ';' + err.toString()); + } if (!(length = parts.length)) { // we added, nothing, must have been an empty string. From b39ba96770caffae339fde41bb398566fa6b4f02 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 13 Aug 2012 18:04:37 -0700 Subject: [PATCH 2/3] fix(): improved exception handling --- src/ng/interpolate.js | 55 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/ng/interpolate.js b/src/ng/interpolate.js index b1c17d99aab9..bb0fbe732652 100644 --- a/src/ng/interpolate.js +++ b/src/ng/interpolate.js @@ -50,7 +50,7 @@ function $InterpolateProvider() { }; - this.$get = ['$parse', function($parse) { + this.$get = ['$parse', '$exceptionHandler', function($parse, $exceptionHandler) { var startSymbolLength = startSymbol.length, endSymbolLength = endSymbol.length; @@ -98,25 +98,20 @@ function $InterpolateProvider() { exp, concat = []; - try { - while(index < length) { - if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) && - ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { - (index != startIndex) && parts.push(text.substring(index, startIndex)); - parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex))); - fn.exp = exp; - index = endIndex + endSymbolLength; - hasInterpolation = true; - } else { - // we did not find anything, so we have to add the remainder to the parts array - (index != length) && parts.push(text.substring(index)); - index = length; - } + while(index < length) { + if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) && + ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { + (index != startIndex) && parts.push(text.substring(index, startIndex)); + parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex))); + fn.exp = exp; + index = endIndex + endSymbolLength; + hasInterpolation = true; + } else { + // we did not find anything, so we have to add the remainder to the parts array + (index != length) && parts.push(text.substring(index)); + index = length; } } - catch (err) { - throw new Error('Error within interpolation: ' + text + ';' + err.toString()); - } if (!(length = parts.length)) { // we added, nothing, must have been an empty string. @@ -127,18 +122,24 @@ function $InterpolateProvider() { if (!mustHaveExpression || hasInterpolation) { concat.length = length; fn = function(context) { - for(var i = 0, ii = length, part; i Date: Mon, 13 Aug 2012 18:13:53 -0700 Subject: [PATCH 3/3] test(): test interpolation with exceptions --- test/BinderSpec.js | 2 +- test/ng/interpolateSpec.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/test/BinderSpec.js b/test/BinderSpec.js index 6d5dd91e0f2f..ba90539f1288 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -175,7 +175,7 @@ describe('Binder', function() { $rootScope.error['throw'] = function() {throw 'MyError';}; errorLogs.length = 0; $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.$apply(); diff --git a/test/ng/interpolateSpec.js b/test/ng/interpolateSpec.js index d5f251ea3d29..05a6f5b26ca6 100644 --- a/test/ng/interpolateSpec.js +++ b/test/ng/interpolateSpec.js @@ -25,6 +25,29 @@ describe('$interpolate', function() { 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'); + })); + + iit('should stop interpolation when encountering an exception', inject(function($interpolate, $compile, $rootScope) { + $rootScope.err = function () { + throw new Error('oops'); + }; + var dom = jqLite('
{{1 + 1}}
{{err()}}
{{1 + 2}}
'); + $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) { $rootScope.name = 'Misko';