framework: Add global error/exception handler
Aurelia should route any uncaught exception or any unhandled rejected promise coming from expressions or from methods called by the framework itself (attached, activate, constructor (DI)) to a global exception handling service in order to let application developers to log the errors.
This would be equivalent to Angular’s $exceptionHandler
service.
This serves two purposes:
- At dev time, I can implement a handler that shows an
alert
whenever there is an uncaught exception. No need to constantly monitor the console window. - At production time, I can implement a handler that forwards all uncaught exceptions to a logging service on my server. Moreover I could decide to show an error panel of some sort, something like “Oups! Something went wrong, please reload your browser.”
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 38 (14 by maintainers)
With all due respect, I do not understand why this issue was closed. The need to present a clean UX to the user when an unhandled error occurs is a necessity in every single app as is the need to log the cause. @EisenbergEffect could please clarify?
Update to my previous post.
Another variation of case 2 is with a failed XHR Request, I was able to implement a workaround using https://github.com/slorber/ajax-interceptor which looks like this.
I still have not found a workaround for Promises.
Until I get Aurelia’s team’s point of view on the issue, I keep the two workarounds inside my custom log appender.
The full code is this:
which is wired-up this way:
I have another component that subscribes to the
Unhandled-Error
message and either shows the error in the view in a red panel or tries to send the error to a logging service on the backend (depending on whether or not my app is running in a debug or release configuration).@zchpit see
window.addEventListener('error'...)
in https://github.com/aurelia/framework/issues/174#issuecomment-173004418I figured out how to handle unhanded promise rejections:
The full code is now:
According to the core-js doc, you also need this in
index.html
. Even without deleting the nativewindow.Promise
it worked with my test case but I guess it is safer to follow the recommendation.