We’re
going to dive into how to capture, handle, and debug Angular errors.
Error handling in vanilla JavaScript consists of using try, catch, and
finally statements. You can also use these
statements in Angular modules. However, Angular has a special logic to handle
uncaught exceptions. We’ll show you how to create custom error handlers for
Angular that you can override to add your own functionality.
Error Logging in Angular
The ErrorHandler class
in Angular 2+ provides a hook for centralized exception handling. The default
implementation of
ErrorHandler
prints
error messages to the console. This service is very simple by design. To
intercept the error handling we need to write a custom handler.
On the other hand, uncaught exceptions in AngularJS are all
funneled through the service. When unmodified,
$exceptionHandler
sends
all uncaught exceptions to the $log.error
service. The $log.error
service
passes the error through to the client’s console.
Here’s how you can create your own error handler:
Angular
2+
class ErrorHandler {
constructor(){}
handleError(error: any): void
}
AngularJS
1.X
$exceptionHandler(exception, [cause]);
0 comments:
Post a Comment