From ae930293cd54b7db2263e689ef782fc14c24f563 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Fri, 16 Mar 2018 08:19:34 +0100 Subject: [PATCH 1/2] fix($compile): throw error in $onChanges immediately This brings it in line with how we throw errors in a digest cycle. Closes https://github.com/angular/angular.js/issues/15578 --- src/ng/compile.js | 5 +---- test/ng/compileSpec.js | 12 +++++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 603d94ed9522..87549124325c 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1562,14 +1562,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { try { onChangesQueue[i](); } catch (e) { - errors.push(e); + $exceptionHandler(e); } } // Reset the queue to trigger a new schedule next time there is a change onChangesQueue = undefined; - if (errors.length) { - throw errors; - } }); } finally { onChangesTtl++; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index e65b951ff90f..ed4b2f97cfc1 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -5075,8 +5075,7 @@ describe('$compile', function() { $rootScope.$apply('a = 42'); // The first component's error should be logged - var errors = $exceptionHandler.errors.pop(); - expect(errors[0]).toEqual(new Error('bad hook')); + expect($exceptionHandler.errors.pop()).toEqual(new Error('bad hook')); // The second component's changes should still be called expect($log.info.logs.pop()).toEqual(['onChange']); @@ -5084,7 +5083,7 @@ describe('$compile', function() { }); - it('should collect up all `$onChanges` errors into one throw', function() { + it('should throw `$onChanges` errors immediately', function() { function ThrowingController() { this.$onChanges = function(change) { throw new Error('bad hook: ' + this.prop); @@ -5113,10 +5112,9 @@ describe('$compile', function() { $rootScope.$apply('a = 42'); - // Both component's error should be logged - var errors = $exceptionHandler.errors.pop(); - expect(errors.pop()).toEqual(new Error('bad hook: 84')); - expect(errors.pop()).toEqual(new Error('bad hook: 42')); + // Both component's error should be logged individually + expect($exceptionHandler.errors.pop()).toEqual(new Error('bad hook: 84')); + expect($exceptionHandler.errors.pop()).toEqual(new Error('bad hook: 42')); }); }); }); From 45dd445509ae3bfc542f8f49359f65a63acc280f Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Fri, 16 Mar 2018 12:05:15 +0100 Subject: [PATCH 2/2] fixup! fix($compile): throw error in $onChanges immediately --- src/ng/compile.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 87549124325c..36b64fe4e41b 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1557,7 +1557,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { } // We must run this hook in an apply since the $$postDigest runs outside apply $rootScope.$apply(function() { - var errors = []; for (var i = 0, ii = onChangesQueue.length; i < ii; ++i) { try { onChangesQueue[i]();