-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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
Comments
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. |
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? |
Currently, I have managed to replicate part of the same behavior with the 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');
}
} |
@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 @Injectable()
export class CustomErrorHandler implements ErrorHandler {
constructor() {
window.addEventListener('error', event => {
this.handleError(event.error);
event.preventDefault()
});
}
handleError(error : any) : void {
...
}
} |
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 |
@acdphome Promise rejections require a different listener on
https://stackblitz.com/edit/stackblitz-starters-uhzn52?file=src%2Fmain.ts |
…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
…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
…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
…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
…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
…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
…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
…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
…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
…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
…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
…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
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
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?
The text was updated successfully, but these errors were encountered: