简体   繁体   English

处理订阅块中的错误而不是 Http 拦截器

[英]Handle error in Subscribe block instead of Http Interceptor

I am calling an api through angular service as below我通过 angular 服务调用 api 如下

this.someservice.save(this.someID, this.payload).subscribe(
  res => { },
  err => { },
  () =>{} );

As per some business rule i am throwing a custom exception from API but when i put a debugger inside error block, it does not show any info, instead it jump to interceptor and show there that info.根据一些业务规则,我从 API 抛出了一个自定义异常,但是当我将调试器放入错误块时,它不显示任何信息,而是跳转到拦截器并在那里显示该信息。

Is there any way that i can receive this info here instead of interceptor inside有什么方法可以让我在这里接收这个信息而不是里面的拦截器

err => { debugger; need too get exception here },

Reason for this is that i am returning a custom error specific only for this angular component (from where i am calling this service) so need to handle error on this component but this project is allready configured with an HTTP interceptor which is catching all the error at one place.原因是我返回了一个自定义错误,仅针对这个 angular 组件(我从这里调用这个服务)所以需要处理这个组件上的错误,但是这个项目已经配置了一个 HTTP 拦截器,它正在捕获所有错误在一处。

You can import from 'rxjs/operators' like this:您可以像这样从 'rxjs/operators' 导入:

import { pipe, catchError } from 'rxjs/operators';

and then you can use it before subscribe like this:然后您可以在订阅之前使用它,如下所示:

this.someService.save(...args).pipe(catchError( err => console.log(err)).subscribe(); 

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

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