[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"
*/
[RxJS] Error Handling in RxJS的更多相关文章
- [RxJS] Error handling operator: catch
Most of the common RxJS operators are about transformation, combination or filtering, but this lesso ...
- [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 ...
- [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 ...
- Erlang error handling
Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...
- MySQL Error Handling in Stored Procedures 2
Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...
- 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 ...
- Error Handling
Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...
- Error Handling and Exception
The default error handling in PHP is very simple.An error message with filename, line number and a m ...
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...
随机推荐
- C#。2. 2 语句
二.语句: 顺序,分支,循环. (一)顺序:略 分支:判断--表达式. if(){} 四大类: 1.if if (age > 18) { Console.WriteLin ...
- div与span
div与span的区别: div标签属于块级元素,span标签属于行内元素,使用对比效果如下: <!DOCTYPE html> <html> <head lang=&qu ...
- 使用PHP对数据库输入进行恶意代码清除
这是一个有用的PHP函数清理了所有的输入数据,并删除代码注入的几率. function sanitize_input_data($input_data) { $input_data = trim(ht ...
- PHP 汉字转拼音(首拼音,所有拼音)
<?php /** +------------------------------------------------------ * PHP 汉字转拼音 +------------------ ...
- destoon实现调用热门关键字的方法
本文所述的destoon调用热门关键字的方法是根据数据库里面的保存的搜索的关键字来显示的.每个模块下面都有各自的关键字下面是调用的标签: ? 1 <!--{tag("moduleid= ...
- Python Udp Socket
socket(套接字),传输层通信的端点,由IP和端口号组成(IP,Port),可以通过socket精确地找到服务器上的进程并与之通信 python2.6实现,基于AF_INET(网络套接字) 类型S ...
- sql delete output
select * into #student1 from student select * from #student1 create table #temp2( id int not null,na ...
- Android4.0强制横屏竖屏
Android的启动默认是横屏或者竖屏我们的TV本来是横屏显示,但是有客户竟然要竖屏显示,昨天快下班收到的需求,竟然说7.19就要搞定.思路有2个,一个就是修改LCD的默认输出,但是这个不是我这个水平 ...
- Chrome下的语音控制框架MyVoix.js使用篇(一)
日前因工作需求,着手研究了语音识别技术,发现github上有网友发布了一款叫做voix.js的javascript框架.在拜读voix.js的源码后发现了不少问题,于是自己写了一款语音识别框架MyVo ...
- Unity3d有关图形尺寸大小的注意事项
主要参考了官方文档,然后根据个人的理解撰写该文.Unity3D支持的图形文件格式有 PSD, TIFF, JPG, TGA, PNG, GIF, BMP, IFF, PICT(但根据本人的亲手测试,U ...