[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 ...
随机推荐
- Ignatius and the Princess III --undo
Ignatius and the Princess III Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (J ...
- 在Debian Wheezy 7.3.0上编译安装3.12.14内核
最近需要对Linux的一个内核模块进行调整实验,故决定先在虚拟机中完成编译调试工作,最后再在真实的系统上进行测试.为了防止遗忘,把过程记录于此. 1. 准备系统环境 首先从官网下载最新版的Virtua ...
- DataGrid( 数据表格) 组件[7]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- XenServer 使用笔记
XenServer 模拟千兆网卡 这两天用 XenServer 安装 VM,其中一台 VM 是用作无盘测试的 Linux Server,不在主流发行版之列,无奈 XenServer 日前对非主流的 L ...
- Visual Studio 2015 RC Downloads
https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs
- javascript 获取图片原始尺寸
javascript 获取图片原始尺寸 function getImgInfo(url){ var img = new Image(), loaded = false; var info = {}; ...
- java学习笔记(3):java的工作原理及相关基础
一.运行机制 如上图所示,图中内容即为Java的运行机制: 1.我们一开始所编写的代码文件存储格式为(如text.java)文件,这就是源程序文件 2.在Java编辑器的作用下,也就是就行了编译,形成 ...
- UVA 10037 贪心算法
题目链接:http://acm.hust.edu.cn/vjudge/contest/122829#problem/A 题目大意:N个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 ...
- Heroku使用
先要生成一个公钥,使用命令:$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save ...
- sql server 数据库附加时程序集错误
在数据库detach和attach的过程中,如果在建立程序集的时候选择的权限集是无限制,并且在建立程序集的时候和后来attach的时候 采用的不是同一个用户,就可能造成部分功能无法使用.原因是由于在选 ...