0

I am trying to return an error message from my service that makes an API call to my component that displays the data.

My service gets data like this:

getResults(searchurl: string): Observable<IPageSpeedResult> {

    return this._http.get(pageSpeedApiUrl)
        .map((response: Response) => this.buildResult(response.json()))
        .do(data => console.log(data))
        .catch((err:Response) => this.handleError(err));
}

I found this answer that talks about how to do it:

Delegation: EventEmitter or Observable in Angular2

So I added these properties to my service:

private _errorReplaySource : ReplaySubject<string> = new ReplaySubject<string>()
public errorReplay : Observable<string> = this._errorReplaySource.asObservable();

Then in my error handling method I called next() on the _errorReplaySource

private handleError(error: Response) {

    this._errorReplaySource.next("There was an error retreiving data for that URL.  Please check it is correct and try again.")

    return Observable.throw(error.json().error || 'Server error');

}

And then in my component I subscibed a property to it:

ngOnInit(): void {

    this._errorSub = this._pageSpeedService.errorReplay.subscribe(error => this.errorMessage = error);

    Observable.combineLatest(
        this._pageSpeedService.getMessageData(),
        this._pageSpeedService.getResults(this.testUrl)
    ).subscribe(
        results => {
            this.messageData = <TestResultMessageLogic[]>results[0];
            this.testResults = results[1];
            this.processApiResponse(this.testResults, this.messageData);
        }
    )
}

In my template I am using {{errorMessage}} but when an error occurs the message is not being passed. I am wondering if I this is even the right technique and what I am doing wrong.

Can someone provide me some advice or a simple example of passing the error message?

Community
  • 1
  • 1
Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • I would override the global error handler, and create a custom error dialog. That way, none of your UI components need to specifically handle errors if you don't want to. They tend to end up being repetitive code anyways. – Michael Kang Mar 15 '17 at 01:34
  • Do you have a link to an example or something? I want to provide feedback to the user inside my component with details about the error. – Guerrilla Mar 15 '17 at 01:47
  • https://angular.io/docs/ts/latest/api/core/index/ErrorHandler-class.html – Michael Kang Mar 15 '17 at 01:50
  • Thanks. So I raise the event on that module and then subscribe to it from my component? Sorry, I am not seeing the full picture here – Guerrilla Mar 15 '17 at 01:54
  • Kinda like this: https://plnkr.co/edit/poukbHB3teVt67OEzKtK?p=preview – Michael Kang Mar 15 '17 at 02:06
  • Why is there errorReplay? It makes everything complicated. Wouldn't it be enough to catch an error in component's subscribe? You're not catching an error in errorReplay subscribe too, that's why errorMessage is not there. – Estus Flask Mar 15 '17 at 02:07
  • @estus I followed the example in the question I linked. I am calling next() and adding string so not sure what you mean about no error. – Guerrilla Mar 15 '17 at 02:10
  • @pixelbits thanks for the plunk. An error dialogue doesn't really suit my purpose, I need to output the information on the page. – Guerrilla Mar 15 '17 at 02:12
  • It doesn't matter if you're calling next() or not if you don't subscribe for error (2nd `subscribe` argument). And again, getResults already throws an error, why is there a need for another observable for errors only? – Estus Flask Mar 15 '17 at 02:28
  • I am subscribing in the ngoninit of my component and assigning it to property. I still don't get what you mean – Guerrilla Mar 15 '17 at 02:31
  • You're subscribing to *value*. And handleError throws an *error*. It should be `.subscribe(results => ..., error => ...)`. – Estus Flask Mar 15 '17 at 09:06
  • I can't spot any errors in your code. I tried to create plunker with something you are trying to do https://plnkr.co/edit/VNTYUs19QC6MIf4NY1to?p=preview – Kyrsberg Mar 15 '17 at 10:26
  • @Kyrsberg did you link the right plunker? that code is not the same – Guerrilla Mar 15 '17 at 11:16
  • @estus the other question says `this._errorReplaySource.next("message")` should generate the event message that I am subscribing to. – Guerrilla Mar 15 '17 at 11:18
  • 1
    Here is the right link https://plnkr.co/edit/jvG92COT6jL8TqExbx9Q?p=preview – Kyrsberg Mar 15 '17 at 11:43
  • @Kyrsberg Thanks. Yes, that looks identical to what I am doing but locally in lite-server it's not working. I cannot work out the difference. – Guerrilla Mar 15 '17 at 13:28
  • @Kyrsberg oh I feel silly. I had typo in my template expression. Thank-you very much for making that plunk. The other comment who said I was doing it wrong made me doubt in the code so thats where I thought the problem lay. Again, many thanks. – Guerrilla Mar 15 '17 at 14:08

0 Answers0