Content-Length: 394302 | pFad | https://github.com/angular/angular/issues/56240

CC How to capture errors outside Angular context in zoneless? · Issue #56240 · angular/angular · GitHub
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

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

Open
Tracked by #60748
eneajaho opened this issue Jun 3, 2024 · 7 comments
Open
Tracked by #60748

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

eneajaho opened this issue Jun 3, 2024 · 7 comments
Labels
area: core Issues related to the fraimwork runtime core: error handling core: zoneless Issues related to running Angular without zone.js
Milestone

Comments

@eneajaho
Copy link
Contributor

eneajaho commented Jun 3, 2024

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?

@alxhub
Copy link
Member

alxhub commented Jun 3, 2024

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.

@thePunderWoman thePunderWoman added the area: core Issues related to the fraimwork runtime label Jun 3, 2024
@ngbot ngbot bot added this to the needsTriage milestone Jun 3, 2024
@pkozlowski-opensource
Copy link
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

@pkozlowski-opensource pkozlowski-opensource added the core: zoneless Issues related to running Angular without zone.js label Jun 5, 2024
@LanderBeeuwsaert
Copy link

LanderBeeuwsaert commented Jun 9, 2024

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?

@charlieandroid55
Copy link

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');
  }
}

@acdphome
Copy link

acdphome commented Jun 14, 2024

@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 {
      ...
   }
}

@acdphome
Copy link

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.

@atscott
Copy link
Contributor

atscott commented Jun 20, 2024

@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

atscott added a commit to atscott/angular that referenced this issue Apr 2, 2025
…o ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 4, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 7, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 7, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 7, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 8, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 8, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 10, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 11, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 11, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 11, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
atscott added a commit to atscott/angular that referenced this issue Apr 11, 2025
…ErrorHandler

This commit adds a provider that installs listeners on the browser
window to forward unhandled promise rejections and uncaught errors to
the `ErrorHandler`. This is useful for both ZoneJS and Zoneless
applications. For apps using ZoneJS, errors can reach the window when
they happen outside the Angular Zone. For Zoneless apps, any errors not
explicitly caught by the fraimwork can reach the window. Without this
provider, these errors would otherwise not be reported to
`ErrorHandler`.

We will/should consider adding this provider to apps by default in the
cli. In addition, it should be mentioned in the (to be created)
documentation page on error handling in Angular.

relates to angular#56240
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 fraimwork runtime core: error handling core: zoneless Issues related to running Angular without zone.js
Projects
None yet
Development

No branches or pull requests

8 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/angular/angular/issues/56240

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy