Allow components to provide custom ErrorHandler
#43504
Labels
area: core
Issues related to the framework runtime
core: error handling
P3
An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Milestone
Which @angular/* package(s) are relevant/releated to the feature request?
common, core
Description
As an Angular developer I want a better DX and UX for handling errors that occur during change detection and event handlers by intercepting errors at the component level.
Currently I am trying to provide the
ErrorHandler
service like so:When I click the button I would expect the
handleError
method to be called. Instead it calls the globalErrorHandler
service provided in the root module. Looking through the source code I noticed that Angular only checks forErrorHandler
in the Module injector when an error occurs.angular/packages/core/src/render3/instructions/shared.ts
Line 2049 in 9b8f056
Proposed solution
When Angular catches an error, it should try injecting the
ErrorHandler
service from the Element injector first. IfErrorHandler
is not found, fall back to using the Module injector. This should also ideally guard against the error handler itself throwing an error by passing the thrown error to the nextErrorHandler
up the Element injector tree. Also consider usingErrorHandler
in place of re-throwing errors in core APIs such asAsyncPipe
.Alternatives considered
The text was updated successfully, but these errors were encountered: