3
3
// #docregion
4
4
// #docregion v1
5
5
import { Injectable } from 'angular2/core' ;
6
- import { Http } from 'angular2/http' ;
6
+ import { Http , Response } from 'angular2/http' ;
7
7
// #enddocregion v1
8
8
// #docregion import-request-options
9
9
import { Headers , RequestOptions } from 'angular2/http' ;
@@ -31,18 +31,13 @@ export class HeroService {
31
31
// #enddocregion endpoint
32
32
33
33
// #docregion methods
34
- // #docregion error-handling
34
+ // #docregion error-handling, http-get
35
35
getHeroes ( ) : Observable < Hero [ ] > {
36
- // #docregion http-get, http-get-v1
37
36
return this . http . get ( this . _heroesUrl )
38
37
. map ( this . extractData )
39
- // #enddocregion v1, http-get-v1, error-handling
40
- . do ( data => console . log ( data ) ) // eyeball results in the console
41
- // #docregion v1, http-get-v1, error-handling
42
38
. catch ( this . handleError ) ;
43
- // #enddocregion http-get, http-get-v1
44
39
}
45
- // #enddocregion error-handling
40
+ // #enddocregion error-handling, http-get
46
41
// #enddocregion v1
47
42
48
43
// #docregion addhero
@@ -62,19 +57,21 @@ export class HeroService {
62
57
63
58
// #docregion v1
64
59
60
+ // #docregion extract-data
65
61
private extractData ( res : Response ) {
66
62
if ( res . status < 200 || res . status >= 300 ) {
67
63
throw new Error ( 'Bad response status: ' + res . status ) ;
68
64
}
69
65
let body = res . json ( ) ;
70
66
return body . data || { } ;
71
67
}
68
+ // #enddocregion extract-data
72
69
73
70
// #docregion error-handling
74
71
private handleError ( error : any ) {
75
- // in a real world app, we may send the error to some remote logging infrastructure
76
- console . error ( error ) ; // log to console instead
72
+ // In a real world app, we might send the error to remote logging infrastructure
77
73
let errMsg = error . message || 'Server error' ;
74
+ console . error ( errMsg ) ; // log to console instead
78
75
return Observable . throw ( errMsg ) ;
79
76
}
80
77
// #enddocregion error-handling
0 commit comments