简体   繁体   English

Rxjs将超时错误从管道抛出到可观察到的错误部分

[英]Rxjs throw timeout error from pipe to observable error section

I'm trying to throw timeout error from the observable pipe to the observer as an error but I am still getting the timeout error in the val callback (in subscribe) while I want to get it in the error callback: 我正在尝试将可观察管道中的超时错误作为错误抛出,但是我仍然想在错误回调中获取val回调(在subscribe中)中的超时错误:

For who is experimented, you can test this code easly and edit it in stackblitz in this link: 对于经过实验的人,您可以轻松地测试此代码并在以下链接的stackblitz中对其进行编辑:

https://stackblitz.com/edit/typescript-eegqyz?file=index.ts&devtoolsheight=100 https://stackblitz.com/edit/typescript-eegqyz?file=index.ts&devtoolsheight=100

of(4000, 3000, 2000)
    .pipe(
        concatMap(duration =>
            makeRequest(duration).pipe(
                timeout(2500),
                catchError(error => {
                    //throwError('Valid token not returned');
                    return of(`Request timed out after: ${duration}`)
                })
            )
        )
    )
    .subscribe(
        val => console.log(val),
        error => console.log("error", error)
    );

Here's how I managed to solve it: 这是我设法解决的方法:

of(4000, 3000, 2000)
  .pipe(
    concatMap(duration =>
      makeRequest(duration).pipe(
        timeout(2500),
        catchError(err => throwError(`Request timed out after: ${duration}`))
      )
    ),
  )

So, basically you have a way to handle errors that come from timeout (which is why you were getting the error in the first cb from subscribe() ) and now if throw another error(using throwError ), there is no other place to handle the incoming errors, so you will get the error in its callback(from subscribe() ). 因此,基本上,您有一种方法来处理timeout所导致的错误(这就是为什么您从subscribe()在第一个cb中得到错误的原因),现在如果抛出另一个错误(使用throwError ),则没有其他地方可以处理传入的错误,因此您将在其回调(从subscribe() )中得到错误。

Here is a StackBlitz example . 这是一个StackBlitz示例

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

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