4

my question is similar to this one How to throw error from RxJS map operator (angular), but I'm on angular6 with rxjs6 and I guess all is changed ;)

I want to know, how I could propagate an Error-Object within map of an observable to the subscribe OnError Part. I always end up in the OnNext Part.

Here is what I have so far:

Within a ng-component I have maybe the following method call

[...]
this.dataStreamService.execCall({ method : 'order_list',params : {}})
   .subscribe( r => {
        // here r provides the result data from http call
        console.log("execCall result", r);
    }, err => {    
        // HERE the "MAP ERROR OCCURED" Error should be occured as well,
        // but in doesn't
        console.log("execCall error",err);

    });
[...]

The called service method looks like:

execCall(dataStreamCall: DataStreamCall): Observable<DataStreamResult> {

    let apiURL = '<some API-URL>';
    let params = dataStreamCall.params;

    // do HTTP request (this.http calls an extra service handler which wraps
    // the angular httpClient and the API errors there
    // There is NO Problem with that part :)
    let apiResult = this.http.post(apiURL, params);

    // Build a new Observable from type "DataStreamResult"
    let dsr : Observable<DataStreamResult> = apiResult
        .pipe(
            map( httpresult => {

                if (httpresult['status'] == false){
                   // the http call was basically successful,
                   // but state in data is false

                   // *** THIS IS NOT PROPAGATE TO SUBSCRIBE OnERROR *** 
                   throwError({'msg' : 'MAP ERROR OCCURED'});

                   // also tried as alternative
                   return throwError({'msg' : 'MAP ERROR OCCURED'});

                } else {

                    // here the http call was successful
                    let d = new DataStreamResult();
                    d.result = httpresult;
                    return d;
                }
            }),
            catchError( err => {
              // error is bubble up from http request handler
              return throwError(err);
            })
        );

    return dsr;
}

Finally the Question: How could manage, that the "throwError" within the piped "map" is propagated to subscribe "err => { ... }".

The actual behavior for:

throwError({..})

I ended up in the subscribe OnNext Part with r = undefined

If I use:

return throwError({..})

I also ended up in the subscribe OnNext Part where r is the throwError-Observable

Thx in Advance Best Regards

Stretau
  • 123
  • 1
  • 9

1 Answers1

3

throwError({'msg' : 'MAP ERROR OCCURED'}) will return an observable that, when subscribed to, will effect an error notification. That is, it will call the subscriber's error method.

In your snippet, you either call throwError and ignore the value. Or you return its return value from a project function passed to the map operator.

Neither will effect an error.

There is no subscriber in the first situation, because the return value is ignored. And, in the second situation, there is no subscriber because the map operator doesn't subscribe to what it receives from the project function - the map operator's project function can return anything; it doesn't have to return an observable.

To throw an error within map, use:

throw {'msg' : 'MAP ERROR OCCURED'};
cartant
  • 57,105
  • 17
  • 163
  • 197
  • Thanks @cartant you made my day. now I understand this things better and the `throw` statement just do what I want :) – Stretau Aug 30 '18 at 07:43