Sometime we want to set a default or fallback value when network request failed.

http$
.pipe(
map(res => Object.values['payload']),
shareReplay(),
catchError(err => of([])) // return an empty value
);

Sometime we want to just throw the error again:

http$
.pipe(
catchError(err => {
return throwError(err)
}), // put catchError and finalize to make sure they will be only run once
finalize(() => console.log('Finalize executed...'))
map(res => Object.values['payload']),
shareReplay()
);

You know about the finally instruction in the try-catch? What if you wanted to have the same when using RxJS Observables? Surprise, there's actually an RxJS operator :wink: called finalizewhich can be used for exactly that. Let's explore in more details.

private execCall(endpoint: string) {
this.isLoading = true;
this.http.get(`/assets/${endpoint}`)
.pipe(
tap(x => console.log('tap', x)),
catchError(err => console.log('did throw') || throwError(err)),
finalize(() => {
this.isLoading = false;
console.log('finalize');
})
)
.subscribe(x => {
console.log('Got result', x);
}, (err) => {
console.error('Got error', err);
})
} /**
did throw
Got error
Error: operators_1.throwError is not a function
finalize
*/

[RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator的更多相关文章

  1. [RxJS 6] The Retry RxJs Error Handling Strategy

    When we want to handle error observable in RxJS v6+, we can use 'retryWhen' and 'delayWhen': const c ...

  2. [RxJS] Error handling operator: catch

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

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  4. Erlang error handling

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

  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. beam 的异常处理 Error Handling Elements in Apache Beam Pipelines

    Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...

  9. Fortify漏洞之Portability Flaw: File Separator 和 Poor Error Handling: Return Inside Finally

    继续对Fortify的漏洞进行总结,本篇主要针对 Portability Flaw: File Separator 和  Poor Error Handling: Return Inside Fina ...

随机推荐

  1. 修改docker-toolbox/boot2docker容器镜像

    进入虚拟机 vi /var/lib/boot2docker/profile 编辑在EXTRA_ARGS,加入 --registry-mirror=https://pee6w651.mirror.ali ...

  2. Kafka的3节点集群详细启动步骤(Zookeeper是外装)

    首先,声明,kafka集群是搭建在hadoop1.hadoop2和hadoop3机器上. kafka_2.10-0.8.1.1.tgz的1或3节点集群的下载.安装和配置(图文详细教程)绝对干货 如下分 ...

  3. Hadoop基础(二)

    HDFS 读写流程 我们知道在HDFS中我们的文件按数据块进行存储,那么当我们写入或者读取一个文件的时候HDFS到底进行了哪些操作呢? HDFS 写流程 如上图所示,假如我们有一个四个节点的集群,并且 ...

  4. css3通过scale()实现放大功能、通过rotate()实现旋转功能

    css3通过scale()实现放大功能.通过rotate()实现旋转功能,下面有个示例,大家可以参考下 通过scale()实现放大功能 通过rotate()实现旋转功能 而transition则可设置 ...

  5. Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事

    Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...

  6. Caffe2:段错误(核心 已转储)

    测试Caffe的时候, cd ~ && python -c 'from caffe2.python import core' 2>/dev/null && ech ...

  7. HDU_1026_Ignatius and the Princess I_BFS(保存路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. 用C#在Visual Studio写Javascript单元测试(Firefox内核)

    引用nuget包: 注意:Geckofx45 nuget包必须是最后引用,否则初始化会出错 编写JsRunner using Gecko; using System; using System.Col ...

  9. pythonGUI编程——Qt库(1)

    1.简单示例实现一个小窗口.       PyQt5是一种高级的语言,下面只有几行代码就能显示一个小窗口.底层已经实现了窗口的基本功能. #!/usr/bin/python#coding:utf-8# ...

  10. Windows下Unity安装

    安装教程: https://www.paws3d.com/lesson/us-0101/ 问题1: 安装并完成注册后,在网页上能登录,但打开Unity时不能启动成功,一直停留在如下界面 解决方案:断网 ...