在订阅中捕获错误是不可能的吗? [英] Is it impossible to catch errors inside a subscription?

查看:38
本文介绍了在订阅中捕获错误是不可能的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular 7,我试图捕获抛出的错误,因为外部 api 返回的值在我期望的值处具有空值.

I am using Angular 7 where I am trying to catch an error thrown because a value returned by the external api had null values where I expected values.

我似乎无法弄清楚在这种情况下我应该做什么.

I can't seem to figure out what I am supposed to do in this scenario.

我有一个调用 api 的服务.

I have a service that call the api.

getObjectValue(param: paramtype): Observable<ObjectValue> {
    return this.http.get<objectwithvalues>(environment.baseapiurl + '/api/object/value/'+ param);
  }

在消费组件中,我像往常一样订阅

In the consuming component I subscribe like I always would

testfunction() {
   this.service.getObjectValue().Subscribe(v => {
    *DO STUFF*
    *Error happens* v['o'] // oh no v is null
   },
    error=>{
     console.log('error') 
     this.snackbar.open('this went wrong' + error); 
    }
  );
}

这是基本情况.这样做不会触发 'error=>{}'Angular 只是简单地吐出一条错误信息.

That is the basic case. Doing this does not trigger 'error=>{}' Angular simply spits out an error message.

所以我在订阅回调范围内尝试了 try/catch.没抓到.

So I tried try/catch within the subscribe callback scope. Did not catch.

我目前正在尝试将服务代码更改为以下内容

I am currently trying to change the service code to the following

getObjectValue(param: paramclass): Observable<ObjectValue> {
    return this.http.get<EventWifi>(
      environment.baseapiurl + '/api/object/value/'+ param).pipe(map(v=> {
   if(v === null){
     return throwError('v is null');  
   } 
  }),
   catchError(err => this.errorTest(err))
);
  }

this.errorTest(err: any): Observable<any> {
 return throwError(err);

}

虽然在订阅者中做同样的事情,但订阅错误函数永远不会捕捉到错误,所以我无法适当地处理错误.

While doing the same in the subscriber, but the error is never caught by the subscribing error function so I am unable to handle the error appropriately.

使用最新显示的服务功能,根本不会抛出任何错误.

With the latest shown service function no error is thrown at all.

我已经尝试了很多配置上的小差异,我现在几乎不记得了,所以一定有一个基本的东西我搞砸了.

I have tried so many small differences in configuration I can hardly remember at this point, so there must be a basic thing I am messing up.

我看过很多例子,但没有一个在我的案例中有效.

I have looked at so many examples but none of them catch and work in my case.

推荐答案

问题是您正在尝试处理订阅功能中的错误.这永远不会达到您的错误功能,很简单,因为它不能.您应该从管道内抛出错误.最好在 mergeMap 中:

The issue is that you are trying to handle the error inside your subscription function. This will never reach your error function, simple because it can't. You should throw the error from within a pipe. Preferably in the mergeMap:

getObjectValue(param: paramtype): Observable<ObjectValue> {
  return this.http.get(environment.baseapiurl + '/api/object/value/'+ param).pipe(
    mergeMap((v) => v == null ? throwError('v is null') : of(v)) 
  );
}

然后你可以这样做:

testfunction() {
  this.service.getObjectValue().subscribe((v) => {
    // v will never be null
  },
  (error) => {
    console.log('error') 
    this.snackbar.open('this went wrong' + error); 
  });
}

如果这在您的应用程序中经常发生,您可以创建一个 HttpInterceptor 来为您执行此操作.如果返回值为空,你可以让它抛出带有特定错误代码和编号的http调用.

If this is something that occurs a lot in your application, you can create a HttpInterceptor which does this for you. You can make it throw the http call with a certain error code and number if the return value is null.

这篇关于在订阅中捕获错误是不可能的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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