0

The control goes to the error block when the inner observable completed execution.

My Component Service Layer - The control goes to the error block.

   this.ApplicationService.getPageData(id).subscribe((DataObj)=>{
      console.log(DataObj);
   },
   (error)=>{
      console.log('Failed to get data.'); <-- The control comes here
   })

In my Application Service Layer -

    return this.dbService.queryData(req).pipe(mergeMap((r)=>{
        if(r!=null) {
           let key : string = r.other_id;
           return dbService.queryOtherSource(key).pipe(map((r1)=> {
                    if(r1) {
                        obj = do some data processing
                        return of(obj); <-- Control comes here.
                    }
                },
                (err)=> {
                    throw err;
                }));
            }            
        },
        (error)=>{
            throw error;
        }));

1 Answer 1

0

MergeMap and Map are not accepting error handling. You are using the mergeMap argument resultSelector that is throwing the error you see.

There is a specific operator to process them. Throw errors when you need to (with a throw new Error or a throwError() from rxjs). If you need to handle it at a specific spot in the Observable chain, use the operator catchError() in the pipe.

See this stackblitz for a working demonstration of error handling based on your code : https://stackblitz.com/edit/angular-h88bjo

There is also no point of rethrowing an error without adding any information. From the moment the error is thrown, it will propagate to the top level if not catched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.