首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular8 HttpInterceptor返回值

Angular8 HttpInterceptor返回值
EN

Stack Overflow用户
提问于 2019-11-18 16:58:54
回答 2查看 1.3K关注 0票数 0

我有一个基本的HttpInterceptor,在其中我使用rxjs retryWhen,以便在出现服务故障时重试一定次数。如果服务调用达到了最大重试量,那么我想将其反馈给最初启动服务调用的方法。

我的问题是,如何将控制权返回给http调用的原始煽动者?我需要这样做,以集中控制处理重试在一个地方(拦截器),我希望能够调用的成功/失败的方法在调用功能。

我的问题是,错误被全局错误处理程序吞没,没有任何东西被传递回我的调用者。

示例:

代码语言:javascript
复制
this.MyServiceCall()
        .pipe(
          map((result) => {
            console.log('this is called when the service returns success');
          }),
         )
         // If there is an error, then how can I show it?
      })
    }


export class HttpRetryInterceptorService implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
        retryWhen(errors => errors
            .pipe(
            concatMap((err:HttpErrorResponse, count) => iif(
            () => (count < 3),
            of(err).pipe(
                map(()=>{
                    console.log(count.toString())
                }),
                delay((2 + Math.random()) ** count * 200)),
                throwError(err)
            ))
        ))
    );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-18 18:48:54

尝试使用catchError()

代码语言:javascript
复制
this.MyServiceCall()
    .pipe(
        map((result) => {
            console.log('this is called when the service returns success');
        }),
        catchError((error) => {
            // Do something and return either an observable or rethrow the error
            return throwError(error);
        })
    );

https://www.learnrxjs.io/operators/error_handling/catch.html

票数 0
EN

Stack Overflow用户

发布于 2019-11-18 18:57:13

我认为您可以使用catchError操作符。

您仍然可以在一个地方(即:您的拦截器)处理错误,并通过在您的服务将订阅的可观察的中抛出一个错误,将错误委托给调用方。

注意事项:如果在拦截器中使用throwError运算符,TS应该会对此提出抱怨:

代码语言:javascript
复制
intercept (req: HttpRequest<any>, next: HttpHandler) {
//~~~~~~ 
    const foo = false;

    return next.handle(req)
    .pipe(
      map(ev => foo ? ev : throwError('err!')),
    )
}

错误是:

类型可观察<从不>不能分配到键入'HttpEvent‘。

HttpEvent类型如下所示:

代码语言:javascript
复制
export type HttpEvent<T> =
    HttpSentEvent | HttpHeaderResponse | HttpResponse<T>| HttpProgressEvent | HttpUserEvent<T>;

所以,不允许出现错误..。但我在this SO post中找到了一个解决办法。

代码语言:javascript
复制
intercept (req: HttpRequest<any>, next: HttpHandler) {
    const foo = false;

    return next.handle(req)
      .pipe(
        map(e => {
          if (e instanceof HttpResponse && !foo) {
            throw new HttpErrorResponse({
              error: 'err'
            });
          }

          return e;
        })
    )
  }

现在,应该在服务的catchError回调中捕获委托错误。

代码语言:javascript
复制
this.MyServiceCall()
    .pipe(
        map((result) => {
            console.log('this is called when the service returns success');
        }),
        catchError(err => {
            // Do something with this error...
        })
    )

编辑

从拦截器抛出错误也可以通过这种方法实现:

代码语言:javascript
复制
intercept (req: HttpRequest<any>, next: HttpHandler) {
    const foo = false;

    return next.handle(req)
      .pipe(
      mergeMap(e => {
        if (!foo) {
          return throwError('err');
        }

        return of(e);
      }),
    )
  }

我错过了这样一个事实:throwError返回了一个可观察的:D。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58919538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档