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.

Using toast in ErrorHandler not working (per directions in FAQ)

See original GitHub issue

ngx toast version: 8.1.1; angular version: 5.0.0; rxjs version: 5.5.2

I’ve verified toastr works in the app, just can’t seem to get it to work as part of my custom error class that extends ErrorHandler.

My class is defined as:

import { ErrorHandler, Inject, Injector} from '@angular/core';
import { ToastrService } from 'ngx-toastr';

export class AppErrorHandler implements ErrorHandler {
    constructor(@Inject(Injector) private injector: Injector) { }

    private get toastrService(): ToastrService {
        return this.injector.get(ToastrService);
    }

    public handleError(error: any): void {
        this.toastrService.error('Show me an error message');
        throw error;
    }
}

and then in root module:

import { AppErrorHandler } from './config/error-handling.config';
@NgModule({
  // ... other imports, declarations, etc.
  providers: [
    { provide: ErrorHandler, useClass: AppErrorHandler },
    // ... other providers
  ]
})
export class AppModule {}

In my component I’m calling a http method with some invalid parameters and then: throw error in the error handler.

.subscribe(
response => { console.log(response) }',
error => { throw error; }
)

I can see my custom error handler class is called but no toast appears. The instructions in the FAQ seem to be a response to ngx-toastr v5.2.4, is there possibly something that was introduced that changes how this should be done?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

24reactions
trevor-hackettcommented, Jan 29, 2018

It should work. In a lot of cases the error handler will run outside of Angular’s zone. This causes toasts not to behave correctly since change detection doesn’t run on it. Turn on onActivateTick in the error handler to ensure that the toast is running inside Angular’s zone:

@Injectable()
export class GlobalErrorHandler extends ErrorHandler {

    constructor(
        @Inject(Injector) private readonly injector: Injector
    ) {
        super(true);
    }

    handleError(error) {
        console.log("Handling error: " + error);
        
        this.toastrService.error("testing", null, { onActivateTick: true })
        
        super.handleError(error);
    }

    /**
     * Need to get ToastrService from injector rather than constructor injection to avoid cyclic dependency error
     * @returns {} 
     */
    private get toastrService(): ToastrService {
        return this.injector.get(ToastrService);
    }

}

This plunker is an example of how to get it to work (using ngx-toastr 8.1.1): https://plnkr.co/edit/0pWbqD?p=preview

2reactions
faiaz000commented, Aug 2, 2019

well i faced the same problem then applied the code as above. But the toster message shows only after i click twice

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 Error Handling not showing toasts - Stack Overflow
I am using the ToastrService in other parts of the application and whenever I call it like I do it here it shows...
Read more >
Notifications, Messages, and Error Handling — part 1 - Medium
When a second toast is triggered while the first is displayed, the first should start disappearing before the second one starts to appear....
Read more >
ngx-toastr - npm
Start using ngx-toastr in your project by running `npm i ngx-toastr`. ... Dependencies. Latest version available for each version of Angular ...
Read more >
Error Handling with Combine and SwiftUI - Peter Friese
Showing an inline error message. This is a good option in case the data the user has provided isn't valid. Not all input...
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
JavaScript allows to use throw with any argument, so technically our custom error ... Our function readUser(json) will not only read JSON, ...
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