[RxJS] Error Handling in RxJS

Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thrown by Observables in RxJS. Operators covered are: catch, onErrorResumeNext, retry and retryWhen.

 

We have the code which throw error when hit 3. This error is catched in error block, so it not go to complete block, but image that we might have some side-effect to handle in complete block instead of just simple log.

复制代码
Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw 'I hate threes';
    }
    return x;
  })
  .subscribe(
    x => console.log(x),
    err => console.error("err: " + err),
    () => console.info('done')
  );

/*
1
2
"err: I hate threes"
*/
复制代码

 

So we need to handle the error and let the code go to the complete block: -- by catch(): 

复制代码
Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw 'I hate threes';
    }
    return x;
  })
  .catch( err => Observable.just('catch: ' + err))
  .subscribe(
    x => console.log(x),
    err => console.error("err: " + err),
    () => console.info('done')
  );

/*
1
2
"catch: I hate threes"
"done"
*/
复制代码

Now the code goes to the complete block and we handle the error by using catch instead of error block. 

 

If we catch the error and still want error block to handle it we can use throw() instead od just():

Observable.throw('catch: ' + err)

 

---------------------

And we use catch(), but we didn't do anything about the error, so if you don't need to handle the error, just throw it, you can use onErrorResumeNext() function.

复制代码
Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw 'I hate threes';
    }
    return x;
  })
  .onErrorResumeNext(Observable.just('There is an error!'))
  .subscribe(
    x => console.log(x),
    err => console.error("err: " + err),
    () => console.info('done')
  );

/*
1
2
"There is an error!"
"done"
*/
复制代码

 

-----------------------------------

Retry(numberofTimes): it will retry number of time before it goes to error. 

 

复制代码
var { Observable } = Rx;
var bad = Observable.throw('go bad');
var good = Observable.just('go ahead!');

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw 'I hate threes';
    }
    return x;
  })
  .retry(3)
  .subscribe(
    x => console.log(x),
    err => console.error(err),
    () => console.info('done')
  );

/*
1
2
1
2
1
2
"I hate threes"

*/
复制代码

 

 ----------------------------

retryWhen(observe): Retry after delay:

复制代码
Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw 'I hate threes';
    }
    return x;
  })
  .retryWhen( errs => errs.delay(1000).take(3))
  .subscribe(
    x => console.log(x),
    err => console.error(err),
    () => console.info('done')
  );

/*
1
2
1
2
1
2
1
2
"done"
*/
复制代码

This it goes to done, because the retryWhen run successfully, so we can concat and error to make it goes to error block:

复制代码
Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw 'I hate threes';
    }
    return x;
  })
  .retryWhen( errs => errs.delay(1000).take(3)
              .concat(Observable.throw("Go error")))
  .subscribe(
    x => console.log(x),
    err => console.error(err),
    () => console.info('done')
  );

/*
1
2
1
2
1
2
1
2
"Go error"
*/
复制代码

 

posted @   Zhentiw  阅读(998)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-03-15 [Whole Web] [Node.js] [Browserify] [Grunt] Automation task with grunt-browserify & grunt-contrib-watch
2015-03-15 [Whole Web] [Node.js] Using npm run to launch local scripts
点击右上角即可分享
微信分享提示