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. (第三章)Java内存模型(上)

    一.java内存模型的基础 1.1 并发编程模型的两个关键问题 在并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来 ...

  2. USB描述符解析-->枚举.

    枚举可以理解为主机按不定的顺序向USB设备讨要设备信息,好给它分配资源,若枚举不成功,就放弃分配资源,免得浪费资源.一般都是使用中断传输方式通信. 常用的描述符有以下几种:01H.设备描述符  02H ...

  3. Python:ajax 学习笔记

    什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...

  4. hdu 1301

    最小生成树模板题 简单的prim算法 AC代码: #include <iostream> #include <stdio.h> #define INF 9999999 usin ...

  5. java对象Integer不能引用传递

    java对象Integer不能引用传递 /** * The value of the <code>Integer</code>. * * @serial */ private ...

  6. Sass函数--数字函数

    数字函数简介 Sass 中的数字函数提要针对数字方面提供一系列的函数功能: percentage($value):将一个不带单位的数转换成百分比值: round($value):将数值四舍五入,转换成 ...

  7. CSS选择器列表

    h1 类型选择器 选择元素的一个类型 .className 类选择器 以class属性的值来选择元素,可以在一个页面中出现多个 #idName ID选择器 以id属性的值来选择元素,在页面中是唯一的, ...

  8. bootstrap table使用小记

    bootstrap table是一个非常不错的,基于bootstrap的插件,它扩展和丰富了bootstrap表格的操作,如格式化表格,表格选择器,表格工具栏,分页等等. 最近基于bootstrap开 ...

  9. Codeforces Round #278 (Div. 1)

    A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang an ...

  10. [Oracle] 浅谈Sequence

    Oracle的Sequence是一种数据库对象,它可以生成有序数字,主要用于主键的自动生成.如果没有Sequence,主键的自动生成必须得在代码逻辑里实现,大致过程是:获取当前主键值,新主键值=当前主 ...