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

fix($compile): throw error in $onChanges immediately #16492

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of the errors variable altogether.

$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