Javascript 在observable或observer中的错误处理?

Javascript 在observable或observer中的错误处理?,javascript,error-handling,promise,rxjs,observable,Javascript,Error Handling,Promise,Rxjs,Observable,我有一个方法(Angular 9,soTypescript),用于检索一个新的Json Web令牌,以验证当前用户的身份 getNewAccessToken(){ return this.httpClient.post<Token>(`${this.baseService.baseUrl}auth-token-refresh/`, { refresh: this.getRefreshToken() }, this.baseService.httpOptions).pipe(

我有一个方法(Angular 9,soTypescript),用于检索一个新的Json Web令牌,以验证当前用户的身份

getNewAccessToken(){
    return this.httpClient.post<Token>(`${this.baseService.baseUrl}auth-token-refresh/`, { refresh: this.getRefreshToken() }, this.baseService.httpOptions).pipe(
      tap((response:Token) => {
        this.cookieService.set(environment.tokenAccessName, response.access, null, '/', null, null, 'Strict');
        this.isLoggedIn.next(true);
      }
  }
我是否可以使用pipecatchError将错误检测直接移动到可观察代码中?代码将转向这一点

getNewAccessToken(){
    return this.httpClient.post<Token>(`${this.baseService.baseUrl}auth-token-refresh/`, { refresh: this.getRefreshToken() }, this.baseService.httpOptions).pipe(
      tap((response:Token) => {
        this.cookieService.set(environment.tokenAccessName, response.access, null, '/', null, null, 'Strict');
        this.isLoggedIn.next(true);
      },
      catchError(error => {
        throw error;
      })
    ));
  }
getNewAccessToken(){
返回此.httpClient.post(`${this.baseService.baseUrl}auth-token-refresh/`,{refresh:this.getrefreshttoken()},this.baseService.httpOptions)。管道(
点击((响应:令牌)=>{
this.cookieService.set(environment.tokenAccessName,response.access,null,“/”,null,null,'Strict');
this.isLoggedIn.next(true);
},
catchError(错误=>{
投掷误差;
})
));
}
我认为这是一种集中管理可观测数据中错误的方法。 一般来说,错误处理在可观察对象上还是在其观察者上更好? 这两种方法的优缺点是什么?在性能方面有什么不同吗?
我认为对于承诺可以提出同样的问题,是的,将错误处理转移到
管道中是一种好的做法,因为它是关注点的分离。它将数据检索与数据表示分离

返回this.http.get(this.heroesUrl)
.烟斗(
catchError(this.handleError('getHeroes',[]))
);

谢谢。但当然,如果我对不同的错误类型有不同的行为,那么我必须在每个订阅调用中分别处理它们,不是吗?此外,您的答案也适用于承诺?@dc_Bita98 no,
this.handleError
应该是处理所有错误的地方。当然,它也适用于承诺
getNewAccessToken(){
    return this.httpClient.post<Token>(`${this.baseService.baseUrl}auth-token-refresh/`, { refresh: this.getRefreshToken() }, this.baseService.httpOptions).pipe(
      tap((response:Token) => {
        this.cookieService.set(environment.tokenAccessName, response.access, null, '/', null, null, 'Strict');
        this.isLoggedIn.next(true);
      },
      catchError(error => {
        throw error;
      })
    ));
  }
return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      catchError(this.handleError('getHeroes', []))
    );