拦截错误并订阅 [英] Intercepting errors and subscribing to

查看:77
本文介绍了拦截错误并订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以正确的方式订阅我的http拦截器时,我遇到了问题.我有以下内容:

I have an issue with subscribing in the right way to my http interceptor. I have the following:

post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>{
     return super.post(url,body, {headers: customHeaders}).catch(err =>{
            console.log('Error caught in POST at '+url);
            return Observable.of(err);
        });
    }

这很好-每次发布时都会记录错误.但是,如果我尝试在其他组件中捕获错误,如下所示:

Which is fine - it logs each time there was an error while posting. However, if I try to catch error in other component like this:

this.http.post('someurl','somestring')
    .subscribe(
        success=>{
            console.log('success: ',success);
        },
        error=>{
            console.log('error: ',error);
        }
    )

现在,当POST中出现错误时,我的控制台日志将打印:

So now, when there is an error in POST my console log prints:

Error caught in POST at someurl
success: //errorobject here//

但是,我期望这样:

Error caught in POST at someurl
error: //errorobject here//

我在做什么错了?

推荐答案

我认为应该是throw err而不是Observable.of(err)

post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>{
     return super.post(url,body, {headers: customHeaders}).catch(err =>{
            console.log('Error caught in POST at '+url);
            throw err;
        });
    }

这篇关于拦截错误并订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆