In most of my API calls I use this pattern:
call server and pipe result
Check for error and throw if needed
map results from server
public getItems() : Observable<Item[]> {
return this.http.get<Item[]>>(environment.url + '/items').pipe( catchError((error:any)=> throwError('Failed getting items')), map((ret)=> this.mapResults(ret)));
}
Is there a way to merge the catchError and map operators to a single operator ?
Something like this:
catchErrorAndMap("Message in case of error",itemForMapping)
itemForMapping will go into the mapping function. something like this?
return this.http.get<Item[]>>(environment.url + '/items').pipe(
catchErrorAndMap('Failed getting items'),ret));