question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Notifications with custom Angular 2 exception handler

See original GitHub issue

Hi, 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:open
  • Created 7 years ago
  • Reactions:1
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
derekmt12commented, Oct 17, 2016

Maybe not the best solution, but I was able to get it working by just wrapping the call in a setTimeout.

handleError(error: any) {
   let unwrappedError = error.originalError || error;
   setTimeout(() => {
      this.notificationsService.error('Error', unwrappedError.message);
   },1);
}
2reactions
Sky4CEcommented, Jul 2, 2018

Can confirm NgZone solution works with angular 5.2.9

handleError(error: any): void {
	this.zone.run(() => this.pageBundle.toast.error("Error", error.message || error));
	super.handleError(error);
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found