I am using Angular 2 HTTP, and have a component subscribing to the response. However, when there is an error, the catch method does not return the error to the subscribed component. It just throws it in the console.
saveFinalize(fcData: LastForecastInterface) {
let responseData = JSON.stringify(fcData);
let body = responseData;
const headers = new Headers();
headers.append('Content-Type', 'application/json;charset=UTF-8');
return this.http.post('/saveFinalize', body, { headers: headers })
.map((data: Response) => data.json())
.catch(this.handleError);
}
public handleError(error: Response | any) {
console.log('err: ', error)
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
// return Observable.throw(errMsg);
return errMsg;
}
And my subscribed component does not get back a response
saveFinalize() {
this.loadingData = true;
return this.httpService.saveFinalize(this.getFc.fcData)
.subscribe(response => {
this.loadingData = false;
console.log(response);
let saveResponse = response.success ? 'Successfully Saved Finalized!' : 'Error Saving Finalized! - ' + response.message;
let respType = response.success ? 'success' : 'danger';
this.alertSvc.showAlert(saveResponse, respType);
});
}