6: Get Data from a Server
[examples/angular-tour-of-heroes] / src / app / hero.service.ts
index d44f22f..75a002b 100644 (file)
@@ -2,8 +2,9 @@ import { Injectable } from '@angular/core';
 import { Observable, of, EMPTY } from 'rxjs';
 import { Hero } from './hero';
 import { HEROES } from './mock-heroes';
-import { HttpClient } from '@angular/common/http';
+import { HttpClient, HttpHeaders } from '@angular/common/http';
 import { MessageService } from './message.service';
+import { catchError, tap } from 'rxjs/operators';
 
 
 @Injectable({
@@ -11,7 +12,7 @@ import { MessageService } from './message.service';
 })
 export class HeroService {
 
-  private heroesUrl = 'api/heroes';  // URL to web api
+  heroesUrl = 'api/heroes';  // URL to web api
 
   constructor(
     private http: HttpClient,
@@ -19,17 +20,46 @@ export class HeroService {
 
   getHeroes(): Observable<Hero[]> {
     this.log('fetching heroes...');
-    return this.http.get<Hero[]>(this.heroesUrl);
+    return this.http
+      .get<Hero[]>(this.heroesUrl)
+      .pipe(
+        tap((heroes: Hero[]) => this.log(`fetched ${heroes.length} heroes`)),
+        catchError(this.handleError<Hero[]>('getHeroes', []))
+      );
   }
 
+  /** GET hero by id. Will 404 if id not found */
   getHero(id: number): Observable<Hero> {
     this.log(`requested hero id=${id}`);
-    const found: Hero | undefined = HEROES.find(hero => hero.id === id);
-    if (found === undefined) {
-      return EMPTY;
-    } else {
-      return of(found);
-    }
+    const url = `${this.heroesUrl}/${id}`;
+    return this.http.get<Hero>(url).pipe(
+      tap(_ => this.log(`fetched hero id=${id}`)),
+      catchError(this.handleError<Hero>(`getHero id=${id}`))
+    );
+  }
+
+  /**
+   * 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 */