Skip to content

How to capture errors outside Angular context in zoneless? #56240

Closed

Description

@eneajaho

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When working with zone.js we can easily capture errors (inside and outside Angular context) in the ErrorHandler using a CustomErrorHandler. When going zoneless we can no longer get both kinds of thrown errors.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-ggzajl?file=src%2Fmain.ts

Please provide the exception or error you saw

When we have zoneless we can listen only to errors thrown in Angular context. 
When we have zone enabled we can listen to all errors.

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

This is probably correct behavior, but is there any way to get these errors through the custom error handler?

Activity

  1. alxhub commented on Jun 3, 2024

    @alxhub
    Member

    There will likely be no replacement for this feature of zones - Angular can tell you about errors that it catches, but can't provide information about errors that happen in other parts of your code that it has no visibility over.

  2. added
    area: core
    on Jun 3, 2024
  3. added this to the needsTriage milestone on Jun 3, 2024
  4. pkozlowski-opensource commented on Jun 5, 2024

    @pkozlowski-opensource
    Member

    Agree with @alxhub - while zone.js might have been doing some error capturing we wouldn't want to replicate the exact mechanism.

    I think that what we might do, though, it to think more about error boundaries, similarly to what is being discussed in #18509

  5. added
    core: zoneless
    on Jun 5, 2024
  6. LanderBeeuwsaert commented on Jun 9, 2024

    @LanderBeeuwsaert

    We see this as a regression in functionality (being able to capture and log any error through ErrorHandler is the foundation of how we keep track of errors that users are having). Would it be possible, as part of the migration of zoneless, to give an official or unofficial guide on how to achieve same or similar functionality as before zoneless?

  7. charlieandroid55 commented on Jun 13, 2024

    @charlieandroid55

    Currently, I have managed to replicate part of the same behavior with the window.onerror listener, which allows me to know when an error occurs, but it does not capture the error itself; an Uncaught error message would still occur and something in your app could stop working, but at least the CustomErrorHandler can notify the error. In my case, I use a toast and when something fails inside or outside Angular context.

    I am not sure if this is the best solution, but it is what I have managed to achieve so far, and I hope it helps someone.

    export class CustomErrorHandler implements ErrorHandler {
    
      constructor() {
        window.onerror = (error: unknown) => {
         this.handleError(error)
        };
      }
    
      handleError(error: unknown): void {
    
        console.info('Start Custom Handler Error');
        console.error('ErrorHandler ->', error);
        console.info('End Custom Handler Error');
      }
    }
  8. acdphome commented on Jun 14, 2024

    @acdphome

    @charlieandroid55: Thanks for this! The following keeps the structure of the error object (instead of just receiving an error string) and really catches the error, so to avoid Uncaught error in the console.

    @Injectable()
    export class CustomErrorHandler implements ErrorHandler {
    	
       constructor() {
          window.addEventListener('error', event => {
             this.handleError(event.error);
             event.preventDefault()
          });
       }
    
       handleError(error : any) : void {
          ...
       }
    }
  9. acdphome commented on Jun 20, 2024

    @acdphome

    Unfortunately, the hack above does not work if we programmatically navigate to a non-existent page, see

    https://stackblitz.com/edit/stackblitz-starters-kvs3fz?file=src%2Fmain.ts

    Since the error happens in the Router service of Angular, the statement made by @alxhub from above needs some clarification with respect to code Angular has no visibility over.

  10. atscott commented on Jun 20, 2024

    @atscott
    Contributor

    @acdphome Promise rejections require a different listener on window

    window.addEventListener('unhandledrejection', (e) => {
          this.handleError(e.reason);
          e.preventDefault();
        });
    

    https://stackblitz.com/edit/stackblitz-starters-uhzn52?file=src%2Fmain.ts

  11. atscott commented on Apr 25, 2025

    @atscott
    Contributor

    Closing as completed by a combination of #60704, angular/angular-cli#30053, and #60848

  12. angular-automatic-lock-bot commented on May 26, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  13. locked and limited conversation to collaborators on May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: error handlingcore: zoneless

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions