bobbyache icon

RxJs - Error Handling

bobbyache | PRO | 03/23/20 05:38:09 PM UTC | 0 ⭐ | 296 👁️ | Never ⏰ | []
JavaScript |

4.63 KB

|

None

|

0 👍

/

0 👎

**************************************************************************************************
Catch and Rethrow
**************************************************************************************************
// Should try and implement catchError as close to the source observable as possible.
...
.pipe(
    catchError(err => {
        console.log("Error occured", err);
        return throwError(err);
    }),
    finalize(() => {
        console.log('Finalize executed');
    }),
    shareReplay()
);
 
**************************************************************************************************
Catch and Retry
**************************************************************************************************
...
.pipe(
  tap(throwOnFirstTry),
  // retry(Infinity)
  retry(1)
);
 
...
.pipe(
  tap(throwOnFirstTry),
  // retry(Infinity)
  retryWhen(() => timer(10000))
);
 
**************************************************************************************************
Catch and Replace
**************************************************************************************************
.pipe(
    catchError(
      err => {
        return of('an error occurred');
      }
    )
);
 
**************************************************************************************************
You Tube - Understanding RxJS Error Handling
https://www.youtube.com/watch?v=KlmABSriUbw
**************************************************************************************************
// 00:17:00 Catch and Retry
// 00:27:50 Catch and Replace
// 00:31:00 Try Catch Inside Operators / Finalize / OnErrorResumeNext
 
 
import { of, from, Infinity, timer } from 'rxjs'; 
import { 
  map, 
  concat, 
  tap,
  catchError, 
  throwError,
  retryWhen,
  retry
} from 'rxjs/operators';
 
console.clear();
 
let thrown = false;
function throwOnFirstTry(data) {
  if (!thrown) {
    thrown = true;
    throw 'Error, timeout';
  }
}
 
let observable = from(['Luke', 'Han', 'Leia'])
.pipe(
  tap(v => console.log(v)),
  tap(throwOnFirstTry),
  // retry(Infinity
  // retryWhen(() => timer(10000))
  catchError((err, obs$) => {
    // conditionally retry because the second argument is the initial
    // observable that errored out.
 
    return obs$;
  })
);
 
observable.subscribe(
  v => console.log(v),
  e => console.error(e)
);
 
/* ********************************************************************************** */
 
import { of, Infinity, timer } from 'rxjs'; 
import { 
  map, 
  concat, 
  tap,
  catchError, 
  throwError,
  retryWhen,
  retry
} from 'rxjs/operators';
 
console.clear();
 
let thrown = false;
function throwOnFirstTry(data) {
  if (!thrown) {
    thrown = true;
    throw 'Error, timeout';
  }
}
 
let observable = of(['Luke', 'Han', 'Leia'])
.pipe(
  tap(throwOnFirstTry),
  // retry(Infinity)
  retryWhen(() => timer(10000))
);
 
observable.subscribe(
  v => console.log(v),
  e => console.error(e)
);
 
/* ********************************************************************************** */
 
import { of } from 'rxjs'; 
import { 
  map, 
  concat, 
  catchError, 
  throwError 
} from 'rxjs/operators';
 
console.clear();
 
let observable = of('joe', 'kevin', 3)
.pipe(
    map(name => {
        return name.toLocaleUpperCase();
    }),
    catchError(
      err => {
        // return of('hi');
        return throwError(err);
      }
    )
);
 
observable.subscribe(
    val => console.log('value', val),
    err => console.log('error', err)
);
 
/* ********************************************************************************** */
 
 
import { of, Infinity } from 'rxjs'; 
import { 
  map, 
  concat, 
  tap,
  catchError, 
  throwError,
  retry
} from 'rxjs/operators';
 
console.clear();
 
let thrown = false;
function throwOnFirstTry(data) {
  if (!thrown) {
    thrown = true;
    throw 'Error, timeout';
  }
}
 
let observable = of(['Luke', 'Han', 'Leia'])
.pipe(
  tap(throwOnFirstTry),
  // retry(Infinity)
  retry(1)
);
 
observable.subscribe(
  v => console.log(v),
  e => console.error(e)
);
 
/* ********************************************************************************** */
 
import { of } from 'rxjs'; 
import { 
  map, 
  concat, 
  catchError, 
  throwError 
} from 'rxjs/operators';
 
console.clear();
 
let observable = of('joe', 'kevin', 3)
.pipe(
    map(name => {
        return name.toLocaleUpperCase();
    }),
    catchError(
      err => {
        // return of('hi');
        return throwError(err);
      }
    )
);
 
observable.subscribe(
    val => console.log('value', val),
    err => console.log('error', err)
);

Comments