You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to capture errors outside Angular context in zoneless? #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
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.
Reacted by Enea Jahollari, Nicolás Cámera, Maksim Popov and Misha Hrokhola
Reacted by Enea Jahollari, CharlieDev, Amos Isaila, LanderBeeuwsaert, Youp Bernoulli and Patric Steiner
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?
Reacted by Beat Hörmann, DiogoLopes99, Youp Bernoulli and Patric Steiner
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.
@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.
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.
Activity
alxhub commentedon 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.
pkozlowski-opensource commentedon Jun 5, 2024
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
LanderBeeuwsaert commentedon 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 commentedon Jun 13, 2024
Currently, I have managed to replicate part of the same behavior with the
window.onerrorlistener, which allows me to know when an error occurs, but it does not capture the error itself; anUncaught errormessage would still occur and something in your app could stop working, but at least theCustomErrorHandlercan 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.
acdphome commentedon 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 errorin the console.acdphome commentedon Jun 20, 2024
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
Routerservice of Angular, the statement made by @alxhub from above needs some clarification with respect to code Angular has no visibility over.atscott commentedon Jun 20, 2024
@acdphome Promise rejections require a different listener on
windowhttps://stackblitz.com/edit/stackblitz-starters-uhzn52?file=src%2Fmain.ts
feat(core): Add provider which reports unhandled errors on window to …
atscott commentedon Apr 25, 2025
Closing as completed by a combination of #60704, angular/angular-cli#30053, and #60848
angular-automatic-lock-bot commentedon May 26, 2025
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.