Boundary: Introduction
Error Boundaries for Angular, with a bit of Suspense.
caution
This package is still being developed, and may contain breaking changes between each release.
Get Started#
Install via NPM
npm install @mmuscat/angular-error-boundaryInstall via Yarn
yarn add @mmuscat/angular-error-boundaryAdd BoundaryModule to your NgModule imports to enable error boundaries.
my.module.ts
@NgModule({ imports: [BoundaryModule], declarations: [MyComponent], ...})export class MyModule {}Add error boundaries to your components.
my.component.html
<error-boundary> <ng-template> <my-widget></my-widget> </ng-template> <fallback>Something went wrong</fallback></error-boundary>Handling async errors#
You should handle any errors that might occur in your Angular application by
injecting the ErrorHandler service.
Calls to the handleError method are intercepted by the nearest error boundary.
class AsyncComponent { value constructor(api: ApiService, errorHandler: ErrorHandler) { this.value = api.thatMaybeFailsAfterSomeTime().pipe( catchError((error) => { errorHandler.handleError(error) return EMPTY }), ) }}With Angular Composition API#
When used with Angular Composition API, errors are automatically caught in the following additional scenarios. No additional setup required.
When
subscribereceives an error notification without an error observer.When a
subscribeobserver throws an errorDuring
ViewDeforServicesetupWhen
listenthrows an error. This is useful for catching errors in event bindings.Example: Event handlerfunction setup() { const eventHandlerThatThrows = listen((event) => { throw new Error("Oops") }) return { eventHandlerThatThrows }} @Component({ template: ` <button (click)="eventHandlerThatThrows($event)">Boom</button> `})