RxJS 系列 – Error Handling Operators
前言
前几篇介绍过了
这篇继续介绍 Error Handling Operators.
参考
RxJS 錯誤處理 Operators (1) - catchError / finalize / retry / retryWhen
Docs – Error Handling Operators

Observable and Subscriber Handler Error
Observable 的流程是 : Observable init > pipe > subscribe
不经过 pipe 的情况下, 处理 error 的方式如下:
const obs = new Observable(subscriber => {
subscriber.error('error message');
// throw 'error message'; // throw 也是可以
});
obs.subscribe({
error: errorMessage => console.log(errorMessage), // error message
});
简单明了.
catchError
catchError 是 pipe 阶段的 error handle. 它是 operator 来的.
catch error and change to succeeded
const obs = new Observable(subscriber => {
subscriber.error('error message');
});
obs
.pipe(
catchError(error => {
console.log(error); // error message
return of('succeeded');
})
)
.subscribe({
next: value => console.log(value), // succeeded
error: errorMessage => console.log(errorMessage), // won't call
});
catchError 接收到 error 后, 可以有几个处理方式. 上面这个是返回一个 "成功" 的 Observable, 这样 error 就不会在传下去了.
catch error and re-throw
catch error 然后继续往下 throw error,
throwError 方法之前介绍过了, 它是 Creation Operators 的一员.
catchError(error => {
return throwError(() => 're-throw error');
// throw 're-throw error'; // 用 throw 也可以
})
小结
总之, catchError 要返回一个 Observable, 可以是 succeeded 或者是 error
catchError(error => {
if (Math.random() < 0.5) {
return throwError(() => 're-throw error');
// throw 're-throw error'; // 用 throw 也可以
} else {
return of('succeeded');
}
})
catch error and retry
除了 succeeded 和 error, 还有一种处理方式是 retry. 所谓 retry 就是 unsubscribe 当前的 stream, 重新 subscribe Observable 得到新的 stream (旧的 Observable 会 displose, 新的会 init)
catchError((error, caught) => {
return caught;
})
返回 catchError 的第二个参数 caught 就表示要 retry. retry 可能会导致死循环的哦.
所以必须要有条件, 避开死循环, 比如:
catchError((error, caught) => {
if (Math.random() < 0.5) {
return caught; // retry
} else {
return of('succeeded');
}
})
delay retry
catchError((error, caught) => {
return timer(2000).pipe(switchMap(() => caught));
})
返回一个 delay 的 Observable 就可以延后 retry 了. 这里用了 switchMap 把 timer 的值换成了 caught observable. (这个 operator 我还没有介绍过的)
retry count
要计算 count 只能开一个外部的 variable 做记入. 或者封装一个自定义 operator. 但更简单的方法是直接用 RxJS 提供的 retry 和 retryWhen operator. 下面会介绍.
retry
用 catchError + caught 实现 retry 太费劲了, 所以 RxJS 封装了 retry operator
retry({
count: 3,
delay: 2000,
resetOnSuccess: false,
})
count 声明可以 retry 多少次
delay 声明 retry 的间隔时间, 有时候 error 是因为 server 繁忙, 只要等 1,2 秒在 retry 发 ajax 就可以解决了.
此外 delay 还支持更复杂的 config. 通过判断 error 和 retryCount 来决定要 delay 多少秒.
我们甚至可以返回 click$ 让 user 点击触发 retry.
也可以直接返回 throwError 结束 retry (即便还没有 hit 到 max retry count), 灵活就对了
delay: (error, retryCount) => timer(2000 * retryCount)
resetOnSuccess 声明当 retry 成功以后是否要 reset retry count. 默认值是 false, 通常 reset 是正确的, 所以一般我都是 set 成 true.
retryWhen
已经废弃了, 改用 retry + delay option 实现吧.

一句话总结
catchError : 在 pipe 中 catch error, 3中处理, 成功, 继续 error, retry
retry : 用 catchError 做 retry 太费劲就有了 retry operator
retryWhen : 废弃了, 改用 retry + delay option 实现.
RxJS 系列 – Error Handling Operators的更多相关文章
- [RxJS] Error handling operator: catch
Most of the common RxJS operators are about transformation, combination or filtering, but this lesso ...
- 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 ...
- MySQL Error Handling in Stored Procedures---转载
This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in store ...
- RxSwift 系列(三) -- Combination Operators
RxSwift 系列(三) -- Combination Operators 前言 本篇文章将要学习如何将多个Observables组合成一个Observable. Combination Opera ...
- Error Handling in ASP.NET Core
Error Handling in ASP.NET Core 前言 在程序中,经常需要处理比如 404,500 ,502等错误,如果直接返回错误的调用堆栈的具体信息,显然大部分的用户看到是一脸懵逼的 ...
随机推荐
- windows安装PHP的redis
一定要先看vc版本和位 配置php的redis扩展 以php7.3 nts版为例,不同的php版本对应不通的redis扩展:下载扩展文件:https://windows.php.net/downloa ...
- [oeasy]python0072_自定义小动物变色_cowsay_color_boxes_asciiart
修改颜色 回忆上次内容 上次搞的是 颜色 前景颜色 总共有 7 种基本色 还有什么 好玩的 么? 可以 给小动物 上色 吗? 配合 先将cowsay结果 输出重定向 sudo apt install ...
- [oeasy]python0067_ESC键进化历史_键盘演化过程_ANSI_控制序列_转义序列_CSI
光标位置 回忆上次内容 上次了解了 新的转义模式 \33 逃逸控制字符 esc 这个字符让输出退出标准输出流 进行控制信息的设置 可以设置光标输出的位置 ASR33中的ALT MODE 是 今天的ES ...
- CF1915B Not Quite Latin Square 题解
CF1915B 题意 给出一个 \(3\) 行 \(3\) 列的字符矩形,其中每行都有字符 ABC 各一个组成,现有一个字符未知,求出未知字符. 思路 就是说每个字符都应该出现 \(3\) 次,所以我 ...
- 搭建php环境
nginx安装在宿主机上 db: 正式采用阿里云rds 测试使用docker安装 注意:报错一般都是需要配置国内镜像源,看之前的配置记录. 除了关闭防火墙,还要设置这个(本地开发环境) 永久关闭 SE ...
- stream流的概述以及idea与stream
前面自己学过一些流的概念,比如IO流,用于读写本地的数据. stream流主要是用于对集合/数组进行操作 idea现在已经很好的支持Stream流操作,在debug的时候可以很好的看到详细内容 下面以 ...
- WordPress基础之主题和插件安装
本篇文章学习WordPress如何安装主题.插件.同时推荐几个我常用的主题.插件及其设置方法. WordPress有海量的主题和插件,有付费的,也有免费的.每个主题都有自己的优缺点,当然,你可以在WP ...
- AI/机器学习(计算机视觉/NLP)方向面试复习1
1. 判断满二叉树 所有节点的度要么为0,要么为2,且所有的叶子节点都在最后一层. #include <iostream> using namespace std; class TreeN ...
- 【RabbitMQ】06 Spring整合RabbitMQ
首先生产者和消费者需要的Maven依赖是一样的 <?xml version="1.0" encoding="UTF-8"?> <project ...
- 使用pycharm专业版(支持远程调试及运行)如何运行mpi的代码呢???(mpi4py的代码)
问题如题: 请注意:这里pycharm专业版的远程调试及运行该如何设置不进行介绍. 由于mpi进程启动是需要执行mpiexec或mpirun命令的,然而在pycharm中我们只能远程调用Python命 ...