前言

前几篇介绍过了

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. CM3调试系统简析

    CM3 调试系统简析 **"一直以来,单片机的调试一直不是很突出的主题,很多简单些的程序在开发中,甚至都没有调试的概念,而只是把生成的映像直接烧入片子,再根据错误症状来判断问题,然后修改程序 ...

  2. 基于MindSpore实现BERT对话情绪识别

    本文分享自华为云社区<[昇思25天学习打卡营打卡指南-第二十四天]基于 MindSpore 实现 BERT 对话情绪识别>,作者:JeffDing. 模型简介 BERT全称是来自变换器的双 ...

  3. [oeasy]python0132_[专业选修]utf-8_unicode_transformation_format_8_编码方式

    utf-8 回忆上次内容 上次再次输出了大红心<span style="color:red"></span> 找到了红心对应的编码 黑红梅方都对应有编码   ...

  4. [oeasy]python0019_ 打包和解包_struct_pack_unpack

    ​ 打包和解包 回忆上次内容 ASCII 由这样几类字符构成 英文大写字符 英文小写字符 数字 符号 电报时代对于英文.数字的编码 使用的是摩斯电码 ​ 编辑 这摩斯电码是3进制的编码方式 长短空 怎 ...

  5. 玄机-第二章日志分析-mysql应急响应

    目录 前言 简介 应急开始 准备工作 日志分析 步骤 1 步骤 2 步骤 3 步骤 4 总结 补充mysql中的/var/log/mysql/erro.log 记录上传文件信息的原因 前言 这里应急需 ...

  6. Pandas库学习笔记(5)---Pandas Panel

    Pandas Panel Pandas Panel基本操作 Panel数据3D容器. 术语 Panel data 源自计量经济学,名称来之于pandas − pan(el)-da(ta)-s. 3个轴 ...

  7. 【教程】重启Windows文件资源管理器

    [教程]重启Windows文件资源管理器 打开任务管理器 以下方法任选其一: 方法一 :组合键 Ctrl + Shift + ESC (个人推荐) 方法二 :组合键 Win + X (或右键Windo ...

  8. XXL-JOB初见

    XXL-JOB是轻量级分布式任务调度平台 port:8088 初始账号:admin/123456 主要有调度中心.执行器.任务 执行流程: 1.执行器向调度中心上报任务 2.调度中心为执行器分配任务 ...

  9. Jmeter函数助手37-setProperty

    setProperty函数用于修改jmeter属性值. 属性名称:填入需要修改的属性名 Value of property:填入需要修改的属性值 Return Original Value of pr ...

  10. NVIDIA的OpenUSD是什么? —— Universal Scene Description (USD)

    正如NVIDIA的老黄在2024年的技术大会上的展示一样,NVIDIA公司或许最准确的定义应该是计算机图形学公司,因为不论是NVIDIA搞GPU还是搞通用计算还是搞软件生态以至于现在搞AI搞机器人搞自 ...