When I use ToastrService in my custom error handler when if there are any error into angular 6 view then cause infinite loop and message no show:
Angular versi贸n: 6.0.8
Toastr version 9.0.2
My code:
import { ToastrService, IndividualConfig } from 'ngx-toastr';
@Injectable()
export class GlobalErrorHandlerService extends ErrorHandler {
constructor(
@Inject(Injector) private injector: Injector) {
super();
}
// Need to get ToastrService from injector rather than constructor injection to avoid cyclic dependency error
private get toastrService(): ToastrService {
return this.injector.get(ToastrService);
}
public handleError(error: any): void {
this.toastRef = this.toastrService.error(
"An unexpected error has occurred.",
"Error",
{
closeButton: true,
timeOut: 5000,
onActivateTick: true,
}
);
super.handleError(error);
}
}
Regards,
This isn't an issue with ngx-toastr.
What is happening is that if there's something like a template error and you've "handled" the error, change detection will continue to run over and over causing the infinite loop. You need to throw an error at the end of the function.
However, since unhandled http errors (and other smaller errors that could be easily handled) will also throw the error at the end and will cause change detection to stop running. The trick is to only throw an error at the end when it's something that you cannot handle or that should cause change detection to stop running (like template errors)
Thanks a lot!
I modified my code and throw an error at the end (throw(error)) and the infinite loop stopped.
public handleError(error: any): void {
this.toastRef = this.toastrService.error(
"An unexpected error has occurred.",
"Error",
{
closeButton: true,
timeOut: 5000,
onActivateTick: true,
}
);
super.handleError(error);
throw(error);
}
Very many thanks again for your help with this.
Most helpful comment
This isn't an issue with ngx-toastr.
What is happening is that if there's something like a template error and you've "handled" the error, change detection will continue to run over and over causing the infinite loop. You need to throw an error at the end of the function.
However, since unhandled http errors (and other smaller errors that could be easily handled) will also throw the error at the end and will cause change detection to stop running. The trick is to only throw an error at the end when it's something that you cannot handle or that should cause change detection to stop running (like template errors)