Handle error of API
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
getUsers(): Observable<User[]>{
return this.http.get('http://reqres.in/api/users')
.map(res => res.json().data)
.catch(this.handleError)
}
private handleError(err){
let errMessage: string;
if(err instanceof Response) {
let body = err.json() || '';
let error = body.error || JSON.stringify(body);
errMessage = `${err.status} - ${err.statusText} || ''} ${error}`;
}else {
errMessage = err.message ? err.message
}
return Observable.throw(errMessage);
}