Notifications with custom Angular 2 exception handler
See original GitHub issueHi, just looking for information/help.
I’m trying to use the notification service along with a custom exception handler class that extends Angular 2’s ExceptionHandler.
@Injectable()
export class MyExceptionHandler extends ExceptionHandler {
constructor(private logger : LoggerService, private notificationService : NotificationsService) {
super(logger);
}
call(error, stackTrace = null, reason = null) {
this.logger.error(error, "Logging the error");
this.notificationService.error("Notify other components", ExceptionHandler.exceptionToString(error));
}
}
MyExceptionHandler is provided on bootstrap so I provide the NotificationsService there also.
bootstrap(AppComponent, [
// Other items...
LoggerService, // Another custom class
NotificationsService,
{provide: ExceptionHandler, useClass: MyExceptionHandler}
]);
However, no notifications are shown when I try to raise an exception in AppComponent. My LoggerService (another custom class) is able to log the exception.
import { Component } from '@angular/core';
import { LoggerService }from './shared/services/logger/logger.service';
import {NotificationsService, SimpleNotificationsComponent} from "angular2-notifications";
@Component({
selector: 'my-app',
directives: [SimpleNotificationsComponent],
template: `
<h1>My First Angular 2 App</h1>
<button (click)="TestError()">Test Error</button>
<simple-notifications></simple-notifications>
`
})
export class AppComponent {
constructor(private logger : LoggerService, private notificationService : NotificationsService) {
}
TestError() {
throw new Error("Test Error");
}
}
Just to confirm everything is setup correctly, I can get notifications to show if I replace the line in TestError() with this:
this.notificationService.error("Error Title", "Error Content");
Am I missing something here or approaching this incorrectly?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:16 (4 by maintainers)
Top Results From Across the Web
Custom error handling for Angular | Rollbar
The default implementation of ErrorHandler prints error messages to the console. This service is very simple by design. To intercept the error ......
Read more >Single Instance Angular 2+ Notification Service / Error Handler
The problem is with the global error handler (provided in the forRoot() in SharedModule, see above), it looks like it might be creating...
Read more >Angular Error Handling Best Practices - Medium
Learn about centralizing Angular error handling by implementing a global error handler, client and server errors, writing good error messages and more.
Read more >Angular 2+ exception handling made simple with logging
We choose to extend the ErrorHandler by a custom class called super.handleError(error) since we want to keep its default functionality of ...
Read more >Angular 2 — Custom Exception Handler | by Netanel Basal
Angular 2 has his own way to catch and handle errors. The default implementation of ErrorHandler prints errors messages to the console. You...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe not the best solution, but I was able to get it working by just wrapping the call in a
setTimeout
.Can confirm NgZone solution works with angular 5.2.9