I've been diving into this tutorial:
https://angular.io/tutorial/toh-pt6#error-handling
I'm struggling to grasp the concept of error-handling
with the function: handleError
.
This function is utilized in the following code snippet:
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
);
}
Here's the definition of the handleError
function:
/**
* Handle Http operation that failed.
* Let the app continue.
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// TODO: send the error to remote logging infrastructure
console.error(error); // log to console instead
// TODO: better job of transforming error for user consumption
this.log(`${operation} failed: ${error.message}`);
// Let the app keep running by returning an empty result.
return of(result as T);
};
}
I am puzzled by the syntax of:
return (error: any): Observable<T> => { ... }
?
Could it be interpreted as:
return ((error: any): Observable<T>) => { ... }
?
I am curious about the origin and purpose of this function.
The more insight you can provide on the inner workings of the handleError
function, the better. I want to delve deep into its logic.