1

I'm chaining Angular Http request as following. The issue is when the first request fails, I get it on error callback, but if the second one fails, nothing happens.

this.myService.requestOne(this.data)
.pipe(mergeMap(dataOne=>{
  return this.myService.requestTwo(dataOne);
}))
.subscribe(dataTwo=>{
  this.loading = false;
},
(err)=>{
  // this fires when only requestOne get failed
  this.loading = false;
});

Any idea?

Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88
  • Because there is no subscriber for `requestTwo`. – brandt.codes Nov 02 '20 at 07:27
  • So, how should be the answer? – Vahid Najafi Nov 02 '20 at 07:33
  • Please take a look here [chaining-rxjs-observables-from-http-data-in-angular2-with-typescript](https://stackoverflow.com/questions/35268482/chaining-rxjs-observables-from-http-data-in-angular2-with-typescript) – brandt.codes Nov 02 '20 at 07:40
  • try to do something like this `this.myService.requestTwo(dataOne).pipe(catchError(err => throwError(err)))`, at that point you're propagating the error throw the pipeline – Shorbagy Nov 02 '20 at 10:12
  • This looks like it should work, I don't think the issue is with your stream. Are you sure requestTwo is failing? – Mrk Sef Nov 02 '20 at 16:37

1 Answers1

0

This isn't really an answer to your question yet, as what you're doing should work. This is some advice that might help you debug what's happening. Here, I'm replacing any errors you get (from either stream) with a new error and a custom message. If an error is thrown, it will get printed to the console.

this.myService.requestOne(this.data).pipe(
  catchError(err => throwError(new Error("Request One Error"))),
  mergeMap(dataOne => 
    this.myService.requestTwo(dataOne).pipe(
      catchError(err => throwError(new Error("Request Two Error")))
    )
  )
).subscribe({
  next: _ => console.log("Stream Emission"),
  complete: () => console.log("Stream Completed"),
  error: err => console.log(err.message)
});

If you're not getting "Request Two Error", that means requestTwo is not throwing an error and your problem is elsewhere. If you get "Stream Completed", then your code ran without throwing an error.

Mrk Sef
  • 7,557
  • 1
  • 9
  • 21
  • What doesn't work? What is happening or not happening? What's being printed to the console? What's the expected behavior? How do you know `requestTwo` is failing? What have you tried? – Mrk Sef Nov 03 '20 at 15:49