在我的Angular 2应用程序中,我有一个服务使用返回Observable的http.get
方法。http.get中的.catch()不会捕获HTTP 500错误代码
campaign.service.ts
public getCampaign(campaignId: string): Observable<Campaign> {
return this.http.get(this.campaignsUrl + '/' + campaignId, options)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || body || {};
}
private handleError(error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
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);
}
然后我把它在我的组件,像这样:
campaign.component.ts
this.campaignService.getCampaign(paramId)
.subscribe(
obj => {
// do something
},
() => {
// handle error here
}
});
但是,如果http.get
调用返回一个HTTP 500,我从来没有打过我的.catch(...)
节y服务班。控制台输出未捕获的异常。
GET http://localhost:9001/api/v1/campaigns/4175edc4 500 (Internal Server Error)
EXCEPTION: Response with status: 500 Internal Server Error for URL: http://localhost:9001/api/v1/campaigns/4175edc4
Uncaught Response {_body: "{"message":"invalid input syntax for uuid: \"4175edc4\""}", status: 500, ok: false, statusText: "Internal Server Error", headers: Headers…}
我在这里做错了什么?
在调试模式下,哪一个被触发; 'this.extractData'或'this.handleError'或者它们都不是?你还可以提供这两种方法吗? – ulubeyn
@ulubeyn,在调试中没有任何命中。我还添加了有关这两种方法的详细信息。 – FilmiHero
错误的控制台输出是什么?当你返回200时,这是否工作? – ulubeyn