[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# Tips:获得当前登录计算机的用户(本地用户/域用户)
须要using的namespace: using System.Security.Principal; 获得登录计算机的用户: WindowsIdentity windowsIdentity = Wi ...
- JavaScript学习(2)
时间就像海绵里的水,要挤总还是有的,所以在最近不是太忙的时间里,我选择了挤时间,一点点的挤出了大把的时间,于是乎便迎来了我对javascript的第二阶段的学习,首先说下, 说起JavaScript大 ...
- 使用Mina框架开发 QQ Android 客户端
Apache MINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可靠性的网络应用程序.它提供了一个通过Java NIO在不同的传输例如TCP/IP和UDP/IP上抽象的事件驱动的异步AP ...
- Redis配置不当可导致服务器被控制,已有多个网站受到影响 #通用程序安全预警#
文章出自:http://news.wooyun.org/6e6c384f2f613661377257644b346c6f75446f4c77413d3d 符合预警中“Redis服务配置不当”条件的服务 ...
- SQL从入门到基础 - 05 数据分组、Having语句
一.数据分组 1. 按照年龄进行分组统计各个年龄段的人数: Select FAge,count(*) from T_Employee group by FAge; 2. Group by子句必须放到w ...
- MySQL sql 执行步骤
基本步骤是 1.from 2.join on 3.where 4.group by 5.having 6.order by 7.select 8.distinct ,sum,... ...
- sqlite3---终端操作
进入沙盒路径 创建stu.sql数据库 sqlite3 stu.sql 创建表 create table if not exists Student (id integer primary key a ...
- 去除input[type=number]最右边的spinners(默认加减符号)
// 去掉input[type=number]默认的加减号 input[type='number'] { -moz-appearance:textfield; } input[type=number] ...
- 关于jdbc Oracle数据库连接的URL错误
今天写了个java类连接oracle,抛出了这个问题 java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@127 ...
- 3月23日html(四) 格式与布局
一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 二.position:absolute 1.外层没有position:absolute(或relat ...