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.

Cannot Inject ToastrService into a custom error handler.

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

17reactions
trevor-hackettcommented, Jan 29, 2018

Your example would be something like the following:

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

@Injectable()
export class AppErrorHandler extends ErrorHandler {

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

  // 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.toastrService.error(
      "An unexpected error has occurred.",
      "Error",
      {
        closeButton: true,
        timeOut: 5000,
        onActivateTick: true
      }
    );

    super.handleError(error);
  }
}

Then in AppModule:

@NgModule({
  // Your imports, declarations, bootstrap, etc.
  // ...

  providers: [
    // Other providers...

    {
      provide: ErrorHandler,
      useClass: AppErrorHandler
    }
  ]
})
export class AppModule {}
1reaction
trevor-hackettcommented, Mar 7, 2018

What you’re trying to do in that issue on stackoverflow is not the same as what is mentioned here. This issue is about trying to inject ToastrService into a custom ErrorHandler. In that SO issue, you’re trying to access ToastrService from a custom error. This is not the same and isn’t possible unless you pass the ToastrService into the constructor of your AccessDenied class. new AccessDenied(toastrService)

I’ve posted an answer on SO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 Toastr Not Working in Global Error Handler
I found this on GitHub - import { ErrorHandler, Injectable, Injector, Inject } from '@angular/core'; import { ToastrService } from ...
Read more >
Error handling - Express.js
Error Handling refers to how Express catches and processes errors that occur both synchronously and asynchronously. Express comes with a default error ...
Read more >
NG0200: Circular dependency in DI detected while ... - Angular
Break this loop (or circle) of dependency to resolve this error. This most commonly means removing or refactoring the dependencies to not be...
Read more >
ngx-toastr - npm
Start using ngx-toastr in your project by running `npm i ngx-toastr`. ... Passed to ToastrService.success/error/warning/info/show() ...
Read more >
Error Handling in Angular - Complete Guide (2022) - YouTube
Yes, we can't get rid of errors completely, however, we can react to them and gracefully handle exceptions improving the user experience i ......
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