Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($compile): throw error in $onChanges immediately
Browse files Browse the repository at this point in the history
This brings it in line with how we throw errors in a digest cycle, 
and also avoids throwing an array.

Closes #15578 
Closes #16492
  • Loading branch information
Narretz authored Mar 22, 2018
1 parent c68b31c commit 983e27b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1557,19 +1557,15 @@ 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]();
} 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++;
Expand Down
12 changes: 5 additions & 7 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5075,16 +5075,15 @@ 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']);
});
});


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);
Expand Down Expand Up @@ -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'));
});
});
});
Expand Down

0 comments on commit 983e27b

Please sign in to comment.