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"
*/

[RxJS] Error Handling in RxJS的更多相关文章

  1. [RxJS] Error handling operator: catch

    Most of the common RxJS operators are about transformation, combination or filtering, but this lesso ...

  2. [RxJS 6] The Retry RxJs Error Handling Strategy

    When we want to handle error observable in RxJS v6+, we can use 'retryWhen' and 'delayWhen': const c ...

  3. [RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator

    Sometime we want to set a default or fallback value when network request failed. http$ .pipe( map(re ...

  4. Erlang error handling

    Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...

  5. MySQL Error Handling in Stored Procedures 2

    Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...

  6. setjmp()、longjmp() Linux Exception Handling/Error Handling、no-local goto

    目录 . 应用场景 . Use Case Code Analysis . 和setjmp.longjmp有关的glibc and eglibc 2.5, 2.7, 2.13 - Buffer Over ...

  7. Error Handling

    Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...

  8. Error Handling and Exception

    The default error handling in PHP is very simple.An error message with filename, line number and a m ...

  9. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

随机推荐

  1. cocos2d-x 2.x 图层特效Effect(转)

    CCSprite* sp = CCSprite::create("Default.png"); sp->setPosition(ccp(, )); addChild(sp); ...

  2. 前端 CSS 基础

    CSS概述 CSS全称:层叠样式表 (Cascading Style Sheets).CSS使得网站可以:创建文档内容清晰地独立于文档表现层. 样式表定义如何显示 HTML 元素,样式通常保存在外部的 ...

  3. 填坑 - 使用Entity Framework 6 + Sqlite进行DB first开发

    Sqlite团队也是渣啊,到第6代了还不支持Code First. 1.安装运行环境和组件 .安装SQLite的Visual Studio设计器支持 只有安装了它,在[新建ADO.NET实体数据模型] ...

  4. 业余编程 SQL 编程学习——1 (SQL Server 2008 R2)

    1.建立test数据库: 2.创建test1数据表: test1表属性如下: 其中,ID字段设置为标识增量,增量种子为1: 这个实例是实现从第一行数据开始,将每一行最后一个字段值加下一行第二个字段值再 ...

  5. struts2 JS获取上传文件的绝对路径,兼容IE和FF

    因为file控件上传失败后会自动清空,所以使用文本框来保存上传路径,而且在不同的浏览器下,控件的样式也需要兼容.下面是自己用到的实例 // 初始化判断浏览器的版本,根据版本的不同使用不同的样式func ...

  6. C# TextBox控件 显示大量数据

    串口通信:在使用TextBox空间显示数据时,因为要显示大量的接收到的数据,当数据量大且快速(串口1ms发送一条数据)时,使用+=的方式仍然会造成界面的卡顿(已使用多线程处理),但使用AppendTe ...

  7. ORACLE主要的系统表和系统视图

    ORACLE主要的系统表和系统视图 1.系统表 ORACLE数据库的系统参数都存储在数据库中,可以通过SQLPLUS,以用户SYS进行查询.几个重要的表或者视图如下: v$controlfile:控制 ...

  8. css3中的提供的元素变化属性

    通过 CSS3 提供的2d元素转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. css3中为我们 提供了: translate() rotate() scale() skew() matrix( ...

  9. PHP正则表达式屏蔽电话号码中间段

    要屏蔽电话号码中间段,首先要知道电话号码的正则表达式. 先来看看PHP匹配电话号码的正则表达式. 匹配固定电话的正则表达式为: /(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\- ...

  10. oracle 主键应用序列和触发器实现自动增长

    oracle 主键自动增长 这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create table simon_example ( id number(4) not null pr ...