代码之家  ›  专栏  ›  技术社区  ›  Maryannah

RxJS:如何抛出一个错误然后捕获它?

  •  1
  • Maryannah  · 技术社区  · 7 年前

    这与我遇到的bug无关,而是语法问题。

    工作流程很简单:

    • 发出一个返回布尔值的HTTP请求
    • 如果布尔值为真,则继续

    为了解决这个问题,我现在的代码是:

    样板

    private _getBoolean() { return this.http.get(...); }
    private _getData() { return this.http.get(...); }
    

    当前代码

    public getData() {
      return this._getBoolean().pipe(
        filter(bool => {
          if(!bool) {
            console.warn('Wrong server answer, stream stopped');
            return false;
          }
          return true;
        }),
        switchMap(bool => this._getData())
      );
    }
    

    我认为会有一些简化语法的东西,像这样

    public getData() {
      return this._getBoolean().pipe(
        throwError(bool => bool ? new Error('Wrong server answer, stream stopped') : null),
        catchError(err => console.warn(err)),
        switchMap(bool => this._getData())
      );
    }
    

    有没有类似的东西,或者我有正确的语法?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Sam Herrmann    7 年前

    考虑下面的可观测值,其发射值为1到4。假设值为3时抛出错误。这个错误可以在 catchError subscribe . 我相信这取决于具体的用例,是让错误一直泡到订阅服务器上,还是应该在订阅服务器上游的某个地方处理。

    of(1, 2, 3, 4).pipe(
      // Throw error when value is 3
      tap(value => { if(value === 3) throw new Error('Oops!') }),
      catchError(err => {
        console.log('Catch error in operator', err);
    
        // You can rethrow the same error or a new error
        // return throwError(err);
    
        // Or you can handle the error and return a new observable
        return of(3)
      })
    ).subscribe(
      value => console.log(value),
      // If `catchError` returns a new observable, then the error 
      // function here will not be called
      err => console.log('Catch error within subscribe', err),
      () => console.log('Done!')
    )
    

    注意,在这个例子中,即使错误被处理,可观察的也会完成,并且值4永远不会被发出。如果您希望在遇到en错误时保持可观察的活动,那么请查看 this StackOverflow answer

        2
  •  0
  •   Tomas    7 年前

    我不确定我是否正确地得到了你的问题,但你可以替换

        console.warn('Wrong server answer, stream stopped');
        return false;
    

       Observable.throw('Some error cause')
    

    然后用最近的 catch -如果重新抛出错误,则停止流 -如果返回可观察的输入,则重新启动它

    public getData() {
      return this._getBoolean().pipe(
        filter(bool => {
          if(!bool) {
            console.warn('Wrong server answer, stream stopped');
            //return false;
            Observable.throw('I got false where I expected true')
          }
          return true;
        }),
        switchMap(bool => this._getData())
      );
    }
    

    然后:

    getData()
    .any()
    .operator()
    .you()
    .wish()
    .catch(e => {
      /* Here stream will be terminated on thrown error */
    })
    
        3
  •  0
  •   tlt    7 年前

    public getData() {
      return this._getBoolean().pipe(
        throwError(bool => bool ? new Error('Wrong server answer, stream stopped') : null),
        catchError(err => console.warn(err)),
        switchMap(bool => this._getData())
      );
    }
    

    为什么不这样做:

    public getData() {
      return this._getBoolean().pipe(
        tap(result => !result && throwError('Wrong server answer, stream stopped')),
        switchMap(bool => this._getData()),
        catchError(err => console.warn(err))
      );
    }
    
    推荐文章