地图HTTP错误响应 [英] Map http error response
本文介绍了地图HTTP错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法来映射不仅成功应对也是错误?
Is there a way to map not only the successful response but also the error?
我要在订阅功能得到修正后的误差
I want to get a modified error in the subscribe function
request.subscribe(
response => {
this.user = response;
},
error => {
this.error = error;
}
);
我已经尝试过这种
I already tried this
let request = this.http.put(url, JSON.stringify(user))
.map(
response => response.json(),
error => this.handleError(error) // returns a string
);
但的HandleError
从未得到执行。
先谢谢了。
推荐答案
要映射错误反应的结果,你需要使用catch操作符:
To map the result of error response, you need to use the catch operator:
let request = this.http.put(url, JSON.stringify(user)).map(
response => response.json())
.catch(
error => this.handleError(error)
);
在地图运营商指定的回调只调用成功的响应。
The callback specified in the map operator is only called for successful responses.
如果你想地图的错误,你可以使用这样的事情:
If you want to "map" the error, you could use something like that:
this.http.get(...)
.map(...)
.catch(res => Observable.throw(res.json())
在这种情况下,被映射的错误将被提供给在订阅方法的第二参数中定义的回调函数。
In this case, the mapped error will be provided to the callback defined in the second parameter of the subscribe method.
这篇关于地图HTTP错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文