1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

CustomErrorHandler Not Catching Exceptions in Angular 18 New Build System

Discussão em 'Angular' iniciado por Asif Zaidi, Outubro 17, 2024 às 14:32.

  1. Asif Zaidi

    Asif Zaidi Guest

    I have a custom error handler in my Angular 18 application that is supposed to catch unhandled exceptions thrown from the backend (ASP.NET). The implementation works fine in the Angular 18 browser build system, but it fails to catch exceptions in the new Angular 18 build system.

    Here’s a simplified version of my CustomErrorHandler:

    import { ErrorHandler, Injectable } from '@angular/core';
    import { Subject } from 'rxjs';

    @Injectable()
    export class CustomErrorHandler implements ErrorHandler {
    private onUnhandledException = new Subject();
    onUnhandledException$ = this.onUnhandledException.asObservable();
    isErrorHandled = false;

    handleError(errorObj) {
    if (this.isErrorHandled) {
    return;
    }

    this.onUnhandledException.next(errorObj);
    }
    }


    In my Handler.service.ts, I subscribe to the onUnhandledException$ observable:

    constructor(
    @Inject(ErrorHandler) private errorHandler: CustomErrorHandler,
    private zone: NgZone
    ) {
    this.errorHandler.onUnhandledException$.subscribe({
    next: (error) => {
    this.shutdownApplication();
    }
    });
    }


    Issue: When an exception is thrown from the backend, it correctly calls handleError() and emits the exceptions in the Angular 18 browser build system. However, in the new Angular 18 build system, the handleError() method does not seem to get triggered at all.

    Questions:

    • What changes in the Angular 18 new build system could be affecting
      the behavior of my custom error handler?
    • How can I ensure that unhandled exceptions are caught by CustomErrorHandler in the new
      build system?
    • Are there any additional configurations or setup steps needed for error handling in Angular 18 with the new build system?

    Any insights or solutions would be greatly appreciated!

    Note: I have also imported zone.js in main.ts file

    Continue reading...

Compartilhe esta Página