简体   繁体   English

您如何处理 Angular 拦截器中的恢复

[英]How do you handle recovery in the Angular Interceptor

How can I recover an intercepted HTTP request in angular?如何在 angular 中恢复截获的 HTTP 请求?

Here is an example of the catch and replace strategy with Observables这是使用 Observables 的捕获和替换策略的示例

https://blog.angular-university.io/rxjs-error-handling/ https://blog.angular-university.io/rxjs-error-handling/

The goal is to catch the error, handle it and return a "success" observable to the original subscriber.目标是捕获错误,处理它并将可观察到的“成功”返回给原始订阅者。 I am not able to get this to work with Angular's interceptor.我无法让它与 Angular 的拦截器一起工作。

This is dumbed down code but if I return an observable it does not hit the success of the original subscriber.这是简化的代码,但如果我返回一个可观察的,它不会影响原始订阅者的成功。 It will hit its complete however.然而,它将完成它。 And the throwError works as expected. throwError 按预期工作。

intercept(
    req: HttpRequest<any>,
    next: HttpHandler
): Observable<HttpEvent<any>> {

return next
  .handle(req)
  .pipe(
    catchError((error, caught) => {
       return of('this does not work');
       //throwError('this works as expected');
    })
  );

} }

interceptor needs to return a Observable<HttpEvent<any>>拦截器需要返回一个Observable<HttpEvent<any>>

try:尝试:

 return of(new HttpResponse({ body: {} }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM