Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow components to provide custom ErrorHandler #43504

Open
antischematic opened this issue Sep 18, 2021 · 1 comment
Open

Allow components to provide custom ErrorHandler #43504

antischematic opened this issue Sep 18, 2021 · 1 comment
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

Comments

@antischematic
Copy link

antischematic commented Sep 18, 2021

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:

@Component({
  template: `
    <button (click)="explode()">Explode</button>
  `,
  providers: [{
    provide: ErrorHandler,
    useExisting: MyComponent
  }]
})
export class MyComponent implements ErrorHandler {
  handleError(error) {
    console.error('in component', error)
  }
  explode() {
    throw new Error("Boom!")
  }
}

When I click the button I would expect the handleError method to be called. Instead it calls the global ErrorHandler service provided in the root module. Looking through the source code I noticed that Angular only checks for ErrorHandler in the Module injector when an error occurs.

const injector = lView[INJECTOR];

Proposed solution

When Angular catches an error, it should try injecting the ErrorHandler service from the Element injector first. If ErrorHandler 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 next ErrorHandler up the Element injector tree. Also consider using ErrorHandler in place of re-throwing errors in core APIs such as AsyncPipe.

Alternatives considered

  • Defensive programming by writing error handling logic in many places, eg. wrapping event handlers in try catch blocks, rxjs catchError operator, etc.
  • Manual change detection to catch template errors.
@Directive()
export class ErrorBoundary implements DoCheck {
  ngDoCheck() {
    try {
      this.cdr.detectChanges()
    }  catch (error) {
      // handle error
    }
  }
}
@jessicajaniuk jessicajaniuk added the area: core Issues related to the framework runtime label Sep 20, 2021
@ngbot ngbot bot added this to the needsTriage milestone Sep 20, 2021
@pkozlowski-opensource
Copy link
Member

Related to #18509

@alxhub alxhub added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Nov 16, 2022
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

4 participants