Using toast in ErrorHandler not working (per directions in FAQ)
See original GitHub issuengx 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:
- Created 6 years ago
- Reactions:1
- Comments:6
Top 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 >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
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: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
well i faced the same problem then applied the code as above. But the toster message shows only after i click twice