Nodon Darkeye
Nodon Darkeye

Reputation: 552

Angular2 Global ErrorHandling Dialog

Angular Global ErrorHandling Dialog

I'm working on an angular2 application and I'm trying to make some global exception handling for it. Basically what I'm trying to do is catch every exception in the application and than have a dialog pop up showing a error title as well as an error message.

I have found That you can just implement your own ErrorHandler and than provide this in your appModule like this:

providers: [{
    provide: ErrorHandler,
    useClass: CustomHandler
}]

Now I was wondering if it's possible to have for example an observable in the errorhandler which the appComponent can subscribe to and perform a function which triggers the display of a dialog screen to show the error. (I'm planning on having custom errors been thrown by my back-end)

I've been having quite a bit of trouble getting something like this set up and haven't been able to find alot of examples of somethings like this so I'm starting to get the feeling this is not really a desired way of dealing with exceptions. Suggestions on how to tackle something like this are welcome.

Upvotes: 1

Views: 1923

Answers (1)

Powkachu
Powkachu

Reputation: 2268

I answered a similar question that might help you: https://stackoverflow.com/a/43610743/5049472

In your case, if you want to add an error, you can do yourService.setError(message: string)

Then the component which displays the error (the one which contains the dialog) has to be subscribed to the service. It will retrieve the error message when another component set it.

An idea on how you can display your dialog:

<dialog *ngIf="showDialog"></dialog>

Assuming this is in the component subscribed to the service.showDialog is a boolean set to false at the beginning. When you get an error message, you set it to true. When the user close the dialog, you set it to false.

Upvotes: 2

Related Questions