前言

前几篇介绍过了

Creation Operators

Filter Operators

Join Creation 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的更多相关文章

  1. [RxJS] Error handling operator: catch

    Most of the common RxJS operators are about transformation, combination or filtering, but this lesso ...

  2. Erlang error handling

    Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...

  3. MySQL Error Handling in Stored Procedures 2

    Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...

  4. 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 ...

  5. Error Handling

    Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...

  6. Error Handling and Exception

    The default error handling in PHP is very simple.An error message with filename, line number and a m ...

  7. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  8. MySQL Error Handling in Stored Procedures---转载

    This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in store ...

  9. RxSwift 系列(三) -- Combination Operators

    RxSwift 系列(三) -- Combination Operators 前言 本篇文章将要学习如何将多个Observables组合成一个Observable. Combination Opera ...

  10. Error Handling in ASP.NET Core

    Error Handling in ASP.NET Core 前言  在程序中,经常需要处理比如 404,500 ,502等错误,如果直接返回错误的调用堆栈的具体信息,显然大部分的用户看到是一脸懵逼的 ...

随机推荐

  1. 算法金 | 秒懂 AI - 深度学习五大模型:RNN、CNN、Transformer、BERT、GPT 简介

    1. RNN(Recurrent Neural Network) 时间轴 1986年,RNN 模型首次由 David Rumelhart 等人提出,旨在处理序列数据. 关键技术 循环结构 序列处理 长 ...

  2. ctfshow sql-labs(笔记)

    这是当时做题的时候记得笔记有些乱看不懂的可以私我 判断闭合方式: id=1' and 1=1–+ *正常回显* id=1' and 1=2–+ *异常回显* id=1 and 1=1 *正常回显* i ...

  3. Notepad++实现代码格式化

    NotePad++是一个轻量级的代码编辑器,占用内存少,运行速度快,Notepad++本身是不带这个格式化功能的,但他支持NppAStyle插件完成格式化. 1. 下载插件NppAStyle.dll, ...

  4. RESTful服务与swagger

    一开始刚学springboot的时候 restful服务+swagger一点都看不懂,现在知识学了一些,再回头看这些东西就简单很多了. 自己跟视频做了一个零件项目,里面写了一些零零散散的模块,其中在视 ...

  5. 对比python学julia(第三章:游戏编程)--(第一节)初识游戏库(3)

    1.1.    键盘和鼠标控制 在游戏应用程序中,通常使用键盘和鼠标作为游戏的操作设备.游戏的窗口都能接收来自键盘和鼠标设备的输人.当用户在键盘上按下按建或释放按键时,会产生相应的键盘事件:当用户移动 ...

  6. 【Python】GUI开发笔记

    一.环境搭建: 1.Pycharm开发工具 pycharm历史版本 https://www.jetbrains.com/pycharm/download/other.html 破解插件 https:/ ...

  7. 【Vue】动态方法调用

    JS的动态方法调用是通过eval函数实现 但是在Vue中是通过组件的$options.methods实现, 写这篇的随笔的原因是因为今天为了封装面包屑组件,花了一下午折腾这个动态方法调用 调用DEMO ...

  8. 使用SSH连接局域网内的WSL Ubuntu

    参考: https://zhuanlan.zhihu.com/p/586283483 https://www.cnblogs.com/lidabo/p/16855975.html ========== ...

  9. 为什么被要求避免使用orcid

    前段时间收到了最高机构的通知,要求不要使用orcid,并对之前使用的情况进行自查.得到这个通知,我其实还是蛮无感的,毕竟我一篇论文也没法过,而且也没有用过这个orcid,虽然我自己也是有这个的. 关于 ...

  10. java零基础到架构师学习线路(附视频教程)

    1.背景 很多人都在问,如何学java,要学那些内容,感觉学起来很痛苦,没得方向,学到什么程度才可以去找工作等, 在这里我以自己的学习经验工作经验和辅导学生的经验给大家梳理了一个学习线路和准备了我自己 ...