[RxJS] Error Handling in RxJS

Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thrown by Observables in RxJS. Operators covered are: catch, onErrorResumeNext, retry and retryWhen.

We have the code which throw error when hit 3. This error is catched in error block, so it not go to complete block, but image that we might have some side-effect to handle in complete block instead of just simple log.

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw ‘I hate threes‘;
    }
    return x;
  })
  .subscribe(
    x => console.log(x),
    err => console.error("err: " + err),
    () => console.info(‘done‘)
  );

/*
1
2
"err: I hate threes"
*/

So we need to handle the error and let the code go to the complete block: -- by catch():

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw ‘I hate threes‘;
    }
    return x;
  })
  .catch( err => Observable.just(‘catch: ‘ + err))
  .subscribe(
    x => console.log(x),
    err => console.error("err: " + err),
    () => console.info(‘done‘)
  );

/*
1
2
"catch: I hate threes"
"done"
*/

Now the code goes to the complete block and we handle the error by using catch instead of error block.

If we catch the error and still want error block to handle it we can use throw() instead od just():

Observable.throw(‘catch: ‘ + err)

---------------------

And we use catch(), but we didn‘t do anything about the error, so if you don‘t need to handle the error, just throw it, you can use onErrorResumeNext() function.

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw ‘I hate threes‘;
    }
    return x;
  })
  .onErrorResumeNext(Observable.just(‘There is an error!‘))
  .subscribe(
    x => console.log(x),
    err => console.error("err: " + err),
    () => console.info(‘done‘)
  );

/*
1
2
"There is an error!"
"done"
*/

-----------------------------------

Retry(numberofTimes): it will retry number of time before it goes to error.

var { Observable } = Rx;
var bad = Observable.throw(‘go bad‘);
var good = Observable.just(‘go ahead!‘);

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw ‘I hate threes‘;
    }
    return x;
  })
  .retry(3)
  .subscribe(
    x => console.log(x),
    err => console.error(err),
    () => console.info(‘done‘)
  );

/*
1
2
1
2
1
2
"I hate threes"

*/

----------------------------

retryWhen(observe): Retry after delay:

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw ‘I hate threes‘;
    }
    return x;
  })
  .retryWhen( errs => errs.delay(1000).take(3))
  .subscribe(
    x => console.log(x),
    err => console.error(err),
    () => console.info(‘done‘)
  );

/*
1
2
1
2
1
2
1
2
"done"
*/

This it goes to done, because the retryWhen run successfully, so we can concat and error to make it goes to error block:

Observable.of(1,2,3,4)
  .map(x => {
    if(x === 3) {
      throw ‘I hate threes‘;
    }
    return x;
  })
  .retryWhen( errs => errs.delay(1000).take(3)
              .concat(Observable.throw("Go error")))
  .subscribe(
    x => console.log(x),
    err => console.error(err),
    () => console.info(‘done‘)
  );

/*
1
2
1
2
1
2
1
2
"Go error"
*/
时间: 2024-10-13 22:32:46

[RxJS] Error Handling in RxJS的相关文章

[RxJS] Error handling operator: catch

Most of the common RxJS operators are about transformation, combination or filtering, but this lesson is about a new category, error handling operators, and its most important operator: catch(). Basic catch( err => Observable): var foo = Rx.Observabl

[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 courses$: Observable<Counse[]> = http$ .pipe( tap(() => console.log("HTTP request")), map(res => Object.values(res['payload'])), share

转 InnoDB Error Handling

14.20.4 InnoDB Error Handling Error handling in InnoDB is not always the same as specified in the SQL standard. According to the standard, any error during an SQL statement should cause rollback of that statement. InnoDB sometimes rolls back only par

Error Handling and Exception

The default error handling in PHP is very simple.An error message with filename, line number and a message describing the error is sent to the browser. PHP has different error handling methods: Simple "die()" statements Custom errors and error t

MySQL Error Handling in Stored Procedures 2

Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in stored procedures. When an error occurs inside a stored procedure, it is important to handle it appropriately, such as continuing or exiting the c

MySQL Error Handling in Stored Procedures---转载

This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in stored procedures. When an error occurs inside a stored procedure, it is important to handle it appropriately, such as continuing or exiting the current co

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, not all errors can be detected at compile time. To create a robust system, each component must be robust. By providing a consistent error-reporting

setjmp()、longjmp() Linux Exception Handling/Error Handling、no-local goto

目录 1. 应用场景 2. Use Case Code Analysis 3. 和setjmp.longjmp有关的glibc and eglibc 2.5, 2.7, 2.13 - Buffer Overflow Vulnerability 1. 应用场景 非局部跳转通常被用于实现将程序控制流转移到错误处理模块中:或者是通过这种非正常的函数返回机制,返回到之前调用的函数中 1. setjmp.longjmp的典型用途是异常处理机制的实现:利用longjmp恢复程序或线程的状态,甚至可以跳过栈中

Error Handling 错误处理

This tutorials aims to teach you how to create an error handler for your programs to deal with the clean-up operation when something in the code goes wrong. by:http://lee-mac.com/errorhandling.html What can go Wrong? Errors can arise in many forms: f