+ return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(
+ tap(x => x.length ?
+ this.log(`found heroes matching "${term}"`) :
+ this.log(`no heroes matching "${term}"`)),
+ catchError(this.handleError<Hero[]>('searchHeroes', []))
+ );
+ }
+
+ /**
+ * 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.
+ if (result === undefined) {
+ return EMPTY as Observable<T>;
+ } else {
+ return of(result as T);
+ }
+ };
+ }
+
+ /** Log a HeroService message with the MessageService */
+ private log(message: string) {
+ this.messageService.add(`HeroService: ${message}`);