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等错误,如果直接返回错误的调用堆栈的具体信息,显然大部分的用户看到是一脸懵逼的 ...
随机推荐
- 薅 AWS 羊毛的船新方式,以 ChatBot 为例
还在担心一年免费服务器到期后该怎么办?(Solo社区 投稿) 网上绝大多数薅 AWS 羊毛的教程都是在教大家如何申请创建一年免费的 VPS,太 OUT 了!就问一个问题,一年到期了那咋办? 其实,除了 ...
- 面向对象VS面向过程
什么是面向对象呢? 对于接触或者熟悉一些编程知识的同学来讲,"面向对象"这个词儿一点儿也不陌生.经常听说XX语言是完全面向对象的编程语言,比如C#.Java这些便是完全面向对象的编 ...
- 用户数据报协议UDP
UDP的首部格式如下: (1) 源端口,源端口号.在需要对方回信时选用.不需要时可用全0. ⑵目的端口,目的端口号.这在终点交付报文时必须使用. ⑶长度,UDP用户数据报的长度,其最小值是8(仅有首部 ...
- 如何解决 CentOS 7 官方 yum 仓库无法使用的问题
一.背景介绍 2024 年 7 月 1 日,在编译基于 CentOS 7.6.1810 镜像的 Dockerfile 过程中,执行 yum install 指令时,遇到了错误:Could not re ...
- Linux安全启动及Machine Owner Key(UEFI BIOS MBR GPT GRUB)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 环境说明 无 前言 只要装过各种系统的人都或多或少会接触 ...
- python统计班级学生
python统计班级学生 如下场景: 假设我有一个学生类和一个班级类,想要实现的功能为: 执行班级人数增加的操作.获得班级的总人数: 学生类继承自班级类,每实例化一个学生,班级人数都能增加 ...
- 银河麒麟V10高级服务器编译安装pg13.4
安装postgres依赖 yum install -y openssl openssl-devel pam pam-devel libxml2 libxml2-devel libxslt libxsl ...
- 【Docker】05 容器操作
[查看容器 Check Container] 查看所有容器: docker ps 查看所有正在运行的,或者运行过的容器: docker ps -a 查看单个指定的容器完整信息: docker insp ...
- Windows/Linux上更新Nessus插件
破解版:http://ximcx.cn/post-151.html 官网文档: https://docs.tenable.com/sccv/Content/OfflineNessusPluginUpd ...
- 如何在CMAKE中指定python路径——使用cmake为python编译扩展模块时指定python路径
答案: cmake -DPython3_EXECUTABLE=/path/to/bin/python3 ================================================ ...