Skip to content

Cannot Inject ToastrService into a custom error handler. #179

Closed
@countzyx

Description

Running the latest version of ngx-toastr with Angular 4.1.2. I'm trying to make a custom error handler that puts out toasts when unexpected errors happen. The code looks like this -

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

export class AppErrorHandler implements ErrorHandler {
constructor(@Inject(ToastrService) private toastrService:ToastrService) { }

handleError(error: any): void {
    this.toastrService.error(
        "An unexpected error has occurred.",
        "Error",
        {
          closeButton: true,
          timeOut: 5000
        }
      )
}

}
`

When I try this I get an error on the console about there being a cyclic dependency while processing the app.module file.
Error: Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1

I suspect that this error is misleading and Angular is just having trouble linking dependencies since the error handler gets loaded pretty early in the startup cycle. Is the problem even fixable? Or is there a way to feed error messages into a queue that can be processed by a service that once loaded can emit toasts? Any help is greatly appreciated.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions