Skip to content

Commit 4a631e1

Browse files
Instalei o json-server no not para acessar a API Rest. Criada ea classe ErrorHandler para lançar exceções ao haver problemas nas requisições http, importado o operador catch para o padrão observable do acesso
1 parent 9da401f commit 4a631e1

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

package-lock.json

Lines changed: 7 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/app.error-handler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Response } from "@angular/http";
2+
import { Observable } from "rxjs/Observable";
3+
4+
export class ErrorHandler {
5+
static handleError(error: Response | any){
6+
let errorMessage: string
7+
if (error instanceof Response){
8+
errorMessage = `Erro ${error.status} ao acessar a URL ${error.url} - ${error.statusText}`
9+
}else{
10+
errorMessage = error.toString()
11+
}
12+
console.log(errorMessage)
13+
return Observable.throw(errorMessage)
14+
15+
}
16+
}

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ROUTES } from './app.routes';
1212
import { RestaurantsComponent } from './restaurants/restaurants.component';
1313
import { RestaurantComponent } from './restaurants/restaurant/restaurant.component';
1414
import { RestaurantService } from './restaurants/restaurant/restaurant.service';
15+
import { RestaurantDetailComponent } from './restaurant/restaurant-detail/restaurant-detail.component';
1516

1617

1718
@NgModule({
@@ -21,7 +22,8 @@ import { RestaurantService } from './restaurants/restaurant/restaurant.service';
2122
HomeComponent,
2223
AboutComponent,
2324
RestaurantsComponent,
24-
RestaurantComponent
25+
RestaurantComponent,
26+
RestaurantDetailComponent
2527
],
2628
imports: [
2729
BrowserModule,

src/app/restaurants/restaurant/restaurant.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import { Injectable } from "@angular/core";
33
import { Http } from "@angular/http";
44
import { MEAT_API } from "app/app.api";
55
import { Observable } from "rxjs/Observable";
6-
import 'rxjs/add/operator/map'
6+
import 'rxjs/add/operator/map';
7+
import 'rxjs/add/operator/catch';
8+
import { ErrorHandler } from "app/app.error-handler";
79

810
@Injectable()
911
export class RestaurantService {
1012

1113
constructor(private http: Http) { }
1214

1315
restaurants(): Observable<Restaurant[]> {
14-
console.log(this.http.get(` Aqui ${MEAT_API}/restaurants`));
1516

16-
return this.http.get('http://localhost:3000/restaurants')
17-
.map(response => response.json());
17+
return this.http.get(`${MEAT_API}/restaurants`)
18+
.map(response => response.json())
19+
.catch(ErrorHandler.handleError)
1820
}
1921
}

0 commit comments

Comments
 (0)