Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inifinite loop on custom errorhandler #564

Closed
javieralfaya opened this issue Sep 10, 2018 · 2 comments
Closed

Inifinite loop on custom errorhandler #564

javieralfaya opened this issue Sep 10, 2018 · 2 comments

Comments

@javieralfaya
Copy link

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,

@trevor-hackett
Copy link
Collaborator

trevor-hackett commented Sep 10, 2018

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)

@javieralfaya
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants