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等错误,如果直接返回错误的调用堆栈的具体信息,显然大部分的用户看到是一脸懵逼的 ...
随机推荐
- el-date-picker的value-forma在Element UI (Vue 2)和Element Plus (Vue 3)中的不同
Element UI (Vue 2): <template> <el-form-item prop="register_date" label="成立日 ...
- 假期小结1学习安装VMware以及linux
学习VMware是一项使我能够创建和管理虚拟机的技能.VMware 是一家知名的虚拟化解决方案供应商,它提供了一系列工具和软件,使我能够在一台物理计算机上创建多个独立的虚拟环境. 首先,我获取了VMw ...
- WordPress基础之主题和插件安装
本篇文章学习WordPress如何安装主题.插件.同时推荐几个我常用的主题.插件及其设置方法. WordPress有海量的主题和插件,有付费的,也有免费的.每个主题都有自己的优缺点,当然,你可以在WP ...
- nats 简介和使用
nats 简介和使用 nats 有 3 个产品 core-nats: 不做持久化的及时信息传输系统 nats-streaming: 基于 nats 的持久化消息队列(已弃用) nats-jetstre ...
- Linux系统下查找安装包所在目录
Linux系统下查找安装包所在目录 想知道Linux系统下安装了哪些软件包,以及软件包安装在哪个目录下,可以用以下命令 1. which which命令查找出相关命令是否已经在搜索路径中,例子如下:$ ...
- app专项测试:app弱网测试
app专项测试:app弱网测试 弱网测试背景 用户体验 APP使用过程中,弱网的高延迟和高丢包,在实时性要求非常高的场景,容易伤害用户体验 非正常情况下,Bug出现几率会增加 在解决日常支持需求中,经 ...
- 【Java】Excel 读写图片工具类
一.需求背景: 做一个大屏管理系统,基础信息包括管理的应用名称,大屏的截图,通过一个excel批量导入 excel的单元格里要插入图片,对应一个大屏应用的信息 导入需要读取到大屏截图,至于存哪还没说. ...
- 【Scala】05 对象特性Part2
特质重复继承关系 父类特质 A 子类特质B 继承 A 子类特质C 继承A 类D 继承了 B 又实现了 C class D extends B with C 继承顺序是 D 继承 C 继承 B 继承 A ...
- ubuntu18.04server系统(cuda11.1)环境下进行mindspore_gpu_1.5版本源码编译
注意: 经过多次尝试发现mindspore_gpu的源码编译必须有sudo权限,否则就会报错. 软硬件环境: 操作系统:Ubuntu18.04.6 (全新系统) CPU:i7 9700k GPU: ...
- hibernate validation,spring validation自定义参数校验
1.背景 在实际开发中,我们除了会使用常用的参数判断,如字符串不为空,最大值,最小值等 我们还可以自定义参数校验规则 2.实际生产问题 实际生产中同步订单的时候, 假设我们要求订单状态值只能是 -1, ...