'rethrowed error isn't caught by global error handler in angular

I have set up a global error handler like this:

@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
  constructor(private zone: NgZone, private router: Router) {}

  handleError(error: any) {
    this.zone.run(() => {
      this.router.navigate(['/error'], {
        skipLocationChange: true,
        state: { error },
      });
    });
  }
}

Then in the app.module I set the provider:

providers: [
    { provide: RouteReuseStrategy, useClass: CustomRouteReuseStrategy },
    { provide: ErrorHandler, useClass: GlobalErrorHandler },
  ]

This works.

I have a shared crud repository class, where I handle the basic crud operations. The update method looks like this:

this.repositoryService.update!(entity).subscribe(
        (id) => {
          this.loadingOverlayVisible = false;
          console.log('finished');
          this.gridData.data = this.gridData.data.map((item) =>
            item.id === entity.id ? entity : item
          );
          this.showNotification(
            'A módosítások sikeresen mentésre kerültek',
            3000,
            'success'
          );
        },
        (errorResp) => {
          this.loadingOverlayVisible = false;
          if (errorResp === typeof HttpErrorResponse) {
            if (errorResp.error.message) {
              this.showNotification(errorResp.error.message, 3000, 'error');
              return;
            } else {
              return throwError(errorResp);
            }
          } else {
            return throwError(errorResp);
          }
        }
      );

As you see if the error is a HttpErrorResponse I show a notification, otherwise I want to pass the error to the global error handler.

The issue is, that the error isn't forwarded to the global error handler (the HttpErrorResponse is handled correctly). If I remove the whole error handling part, the global error handlig catches the error.

How should I modify the code, that the global error handler catches any other errors?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source