Skip to content

Commit 7b819be

Browse files
atscottthePunderWoman
authored andcommitted
fix(core): Ensure errors in listeners report to the application error handler (#60251)
Practically speaking, this change ensures that Angular is able to make decisions about what to do when an uncaught error happens. For tests, this will mean that, by default, the error either causes the `fixture.whenStable` promise to reject if there is one or rethrow the error. This ensures tests do not accidentally ignore errors. Opting out of this can be done with the `rethrowApplicationErrors: false` option in `TestBed`. For SSR, there may be additional behaviors in the future that we want to add, such as redirecting to an error page or responding with a 500 status code. BREAKING CHANGE: Uncaught errors in listeners which were previously only reported to `ErrorHandler` are now also reported to Angular's internal error handling machinery. For tests, this means that the error will be rethrown by default rather than only logging the error. Developers should fix these errors, catch them in the test if the test is intentionally covering an error case, or use `rethrowApplicationErrors: false` in `configureTestingModule` as a last resort. PR Close #60251
1 parent eb97303 commit 7b819be

File tree

4 files changed

+5
-15
lines changed

4 files changed

+5
-15
lines changed

packages/core/src/render3/view/listeners.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import {
2121
} from '../util/view_utils';
2222
import {profiler} from '../profiler';
2323
import {ProfilerEvent} from '../profiler_types';
24-
import {ErrorHandler} from '../../error_handler';
2524
import {markViewDirty} from '../instructions/mark_view_dirty';
2625
import {RElement, RNode} from '../interfaces/renderer_dom';
2726
import {GlobalTargetResolver, Renderer} from '../interfaces/renderer';
2827
import {assertNotSame} from '../../util/assert';
28+
import {handleUncaughtError} from '../instructions/shared';
2929

3030
/** Shorthand for an event listener callback function to reduce duplication. */
3131
export type EventCallback = (event?: any) => any;
@@ -95,27 +95,14 @@ function executeListenerWithErrorHandling(
9595
// Only explicitly returning false from a listener should preventDefault
9696
return listenerFn(e) !== false;
9797
} catch (error) {
98-
// TODO(atscott): This should report to the application error handler, not the ErrorHandler on LView injector
99-
handleError(lView, error);
98+
handleUncaughtError(lView, error);
10099
return false;
101100
} finally {
102101
profiler(ProfilerEvent.OutputEnd, context, listenerFn);
103102
setActiveConsumer(prevConsumer);
104103
}
105104
}
106105

107-
/**
108-
* Handles an error thrown in an LView.
109-
* @deprecated Use handleUncaughtError to report to application error handler
110-
*/
111-
function handleError(lView: LView, error: any): void {
112-
const injector = lView[INJECTOR];
113-
if (!injector) {
114-
return;
115-
}
116-
injector.get(ErrorHandler, null)?.handleError(error);
117-
}
118-
119106
/**
120107
* Listen to a DOM event on a specific node.
121108
* @param tNode TNode on which to listen.

packages/core/test/acceptance/create_component_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ describe('createComponent', () => {
11731173
}
11741174

11751175
TestBed.configureTestingModule({
1176+
rethrowApplicationErrors: false,
11761177
providers: [
11771178
{
11781179
provide: ErrorHandler,

packages/core/test/acceptance/listener_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ describe('event listeners', () => {
440440
}
441441

442442
TestBed.configureTestingModule({
443+
rethrowApplicationErrors: false,
443444
declarations: [TestCmpt, LikesClicks, ThrowsOnClicks],
444445
providers: [{provide: ErrorHandler, useClass: CountingErrorHandler}],
445446
});

packages/core/test/acceptance/profiler_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ describe('profiler', () => {
175175
const errorSpy = spyOn(handler, 'handleError');
176176

177177
TestBed.configureTestingModule({
178+
rethrowApplicationErrors: false,
178179
declarations: [MyComponent],
179180
providers: [{provide: ErrorHandler, useValue: handler}],
180181
});

0 commit comments

Comments
 (0)