[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 ...
随机推荐
- 金典 SQL笔记(4)
由于在本地笔记上写的.CSDN markdown编辑器仅仅支持.md格式导入, 图片没办法直接导进去.写的多了懒的一张一张图片切图上传; 直接整个文章切成图片上传上去了. watermark/2/te ...
- Drawable与Bitmap 自定义
Drawable简介 Drawable是Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF.PNG.JPG,当然也支持BMP.相比于View,我们并不需要去考虑如何measure. ...
- Java多线程练习三
public class ex5 { public static void main(String [] args) { thread5 t1 = new thread5(); thread5_1 t ...
- HTML基础总结<文本格式>
HTML 文本格式化标签 标签 描述 <b> 定义粗体文本 <em> 呈现为被强调的文本 <i> 定义斜体字 <small> 定义小号字 <str ...
- 关于jdbc Oracle数据库连接的URL错误
今天写了个java类连接oracle,抛出了这个问题 java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@127 ...
- shell中对于命令的搜寻顺序
当你在shell命令行输入一条命令时,shell的搜寻顺序是如何的呢?当你的脚本名字和shell中的函数名字重名,shell是如何决定运行哪一个的? 在shell中,shell对于命令的搜寻优先级为: ...
- 【vc】5_文本编程
1.插入符(Caret): (1) 文本插入符 函数的原型声明:(CWnd类) void CreateSolidCaret ( int Nwidth, int nHeight ); ·nwidth:指 ...
- ie6下:png图片不透明 和 背景图片为png的节点的内部标签单击事件不响应
1.png图片不透明 少量图片时:使用滤镜: _background:none; _filter:prodig:DXImageTransform.Microsoft.AlphaImageLoader( ...
- Caffe : Layer Catalogue(2)
TanH / Hyperbolic Tangent 类型(type):TanH CPU 实现: ./src/caffe/layers/tanh_layer.cpp CUDA.GPU实现: ./src/ ...
- cf B. The Fibonacci Segment
http://codeforces.com/contest/365/problem/B #include <cstdio> #include <cstring> #includ ...