rmnng
rmnng

Reputation: 69

How to catch failed and canceled request in RxJs Observable?

Since a while, I noticed that I had a few XHR that end with no HTTP status but with canceled or failed status.

It hapends on Chrome. On Firefox I didn't manage to have it.

My app works with Angular 4 and RxJs 5.

Is there any solution that could help me to catch "this errors". I call it "errors" but there're not really errors because if there were I could catch them easily.

In RxJs, when I use catch it doesn't trigger, neither do finally (or it does but after my subscription ends). I also tried with forkJoin, zip or stuff like concat, flatMap or switchMap, but nothing seems to work in any case.

Upvotes: 0

Views: 986

Answers (2)

Prasanna
Prasanna

Reputation: 1751

As mentioned in your comment catch won't trigger as you face XSS issue while displaying those content on View page(i.e .html).I think you need to use DomSanitizer.

DomSanitizer helps preventing Cross Site Scripting Security bugs (XSS) by sanitizing values to be safe to use in the different DOM contexts(Detail description of DomSanitization)

In your component.ts file use

import { DomSanitizer } from '@angular/platform-browser';

this.yourVariable = domsanitizer.bypassSecurityTrustHtml('<p>stack Overflow</p>');

and use yourVariable in view as follows <div [innerHtml]="yourVariable"></div>

This won't give you error on chrome.

Upvotes: 2

Fan Cheung
Fan Cheung

Reputation: 11360

I am not sure whether a canceled request will trigger an error status but alternatively you can add a timeout to the observable chain which will fire an error at the end.

http.get(..).timeout(2000).catch()

Upvotes: 0

Related Questions