bobbyache

RxJs - Error Handling

Mar 23rd, 2020
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. **************************************************************************************************
  2. Catch and Rethrow
  3. **************************************************************************************************
  4. // Should try and implement catchError as close to the source observable as possible.
  5. ...
  6. .pipe(
  7.     catchError(err => {
  8.         console.log("Error occured", err);
  9.         return throwError(err);
  10.     }),
  11.     finalize(() => {
  12.         console.log('Finalize executed');
  13.     }),
  14.     shareReplay()
  15. );
  16.  
  17. **************************************************************************************************
  18. Catch and Retry
  19. **************************************************************************************************
  20. ...
  21. .pipe(
  22.   tap(throwOnFirstTry),
  23.   // retry(Infinity)
  24.   retry(1)
  25. );
  26.  
  27. ...
  28. .pipe(
  29.   tap(throwOnFirstTry),
  30.   // retry(Infinity)
  31.   retryWhen(() => timer(10000))
  32. );
  33.  
  34. **************************************************************************************************
  35. Catch and Replace
  36. **************************************************************************************************
  37. .pipe(
  38.     catchError(
  39.       err => {
  40.         return of('an error occurred');
  41.       }
  42.     )
  43. );
  44.  
  45. **************************************************************************************************
  46. You Tube - Understanding RxJS Error Handling
  47. https://www.youtube.com/watch?v=KlmABSriUbw
  48. **************************************************************************************************
  49. // 00:17:00 Catch and Retry
  50. // 00:27:50 Catch and Replace
  51. // 00:31:00 Try Catch Inside Operators / Finalize / OnErrorResumeNext
  52.  
  53.  
  54. import { of, from, Infinity, timer } from 'rxjs';
  55. import {
  56.   map,
  57.   concat,
  58.   tap,
  59.   catchError,
  60.   throwError,
  61.   retryWhen,
  62.   retry
  63. } from 'rxjs/operators';
  64.  
  65. console.clear();
  66.  
  67. let thrown = false;
  68. function throwOnFirstTry(data) {
  69.   if (!thrown) {
  70.     thrown = true;
  71.     throw 'Error, timeout';
  72.   }
  73. }
  74.  
  75. let observable = from(['Luke', 'Han', 'Leia'])
  76. .pipe(
  77.   tap(v => console.log(v)),
  78.   tap(throwOnFirstTry),
  79.   // retry(Infinity
  80.   // retryWhen(() => timer(10000))
  81.   catchError((err, obs$) => {
  82.     // conditionally retry because the second argument is the initial
  83.     // observable that errored out.
  84.  
  85.     return obs$;
  86.   })
  87. );
  88.  
  89. observable.subscribe(
  90.   v => console.log(v),
  91.   e => console.error(e)
  92. );
  93.  
  94. /* ********************************************************************************** */
  95.  
  96. import { of, Infinity, timer } from 'rxjs';
  97. import {
  98.   map,
  99.   concat,
  100.   tap,
  101.   catchError,
  102.   throwError,
  103.   retryWhen,
  104.   retry
  105. } from 'rxjs/operators';
  106.  
  107. console.clear();
  108.  
  109. let thrown = false;
  110. function throwOnFirstTry(data) {
  111.   if (!thrown) {
  112.     thrown = true;
  113.     throw 'Error, timeout';
  114.   }
  115. }
  116.  
  117. let observable = of(['Luke', 'Han', 'Leia'])
  118. .pipe(
  119.   tap(throwOnFirstTry),
  120.   // retry(Infinity)
  121.   retryWhen(() => timer(10000))
  122. );
  123.  
  124. observable.subscribe(
  125.   v => console.log(v),
  126.   e => console.error(e)
  127. );
  128.  
  129. /* ********************************************************************************** */
  130.  
  131. import { of } from 'rxjs';
  132. import {
  133.   map,
  134.   concat,
  135.   catchError,
  136.   throwError
  137. } from 'rxjs/operators';
  138.  
  139. console.clear();
  140.  
  141. let observable = of('joe', 'kevin', 3)
  142. .pipe(
  143.     map(name => {
  144.         return name.toLocaleUpperCase();
  145.     }),
  146.     catchError(
  147.       err => {
  148.         // return of('hi');
  149.         return throwError(err);
  150.       }
  151.     )
  152. );
  153.  
  154. observable.subscribe(
  155.     val => console.log('value', val),
  156.     err => console.log('error', err)
  157. );
  158.  
  159. /* ********************************************************************************** */
  160.  
  161.  
  162. import { of, Infinity } from 'rxjs';
  163. import {
  164.   map,
  165.   concat,
  166.   tap,
  167.   catchError,
  168.   throwError,
  169.   retry
  170. } from 'rxjs/operators';
  171.  
  172. console.clear();
  173.  
  174. let thrown = false;
  175. function throwOnFirstTry(data) {
  176.   if (!thrown) {
  177.     thrown = true;
  178.     throw 'Error, timeout';
  179.   }
  180. }
  181.  
  182. let observable = of(['Luke', 'Han', 'Leia'])
  183. .pipe(
  184.   tap(throwOnFirstTry),
  185.   // retry(Infinity)
  186.   retry(1)
  187. );
  188.  
  189. observable.subscribe(
  190.   v => console.log(v),
  191.   e => console.error(e)
  192. );
  193.  
  194. /* ********************************************************************************** */
  195.  
  196. import { of } from 'rxjs';
  197. import {
  198.   map,
  199.   concat,
  200.   catchError,
  201.   throwError
  202. } from 'rxjs/operators';
  203.  
  204. console.clear();
  205.  
  206. let observable = of('joe', 'kevin', 3)
  207. .pipe(
  208.     map(name => {
  209.         return name.toLocaleUpperCase();
  210.     }),
  211.     catchError(
  212.       err => {
  213.         // return of('hi');
  214.         return throwError(err);
  215.       }
  216.     )
  217. );
  218.  
  219. observable.subscribe(
  220.     val => console.log('value', val),
  221.     err => console.log('error', err)
  222. );
Advertisement
Add Comment
Please, Sign In to add comment