Skip to content

Instantly share code, notes, and snippets.

@PhiLhoSoft
Last active February 14, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhiLhoSoft/3d4b162b8094a95479fc1b67dc41059f to your computer and use it in GitHub Desktop.
Save PhiLhoSoft/3d4b162b8094a95479fc1b67dc41059f to your computer and use it in GitHub Desktop.
RxJS error management exploration
// index.ts as typed in https://stackblitz.com/edit/rxjs-iqjrzu
import { of, throwError } from 'rxjs';
import { map, switchMap, catchError, tap, finalize } from 'rxjs/operators';
const source = of('World').pipe(
map(x => { return { good: `Hello ${x}!` }; }),
// Comment out for testing regular behavior
switchMap((x) => throwError('bad')),
);
source.pipe(
// Comment out to get subscribe's error handling
//*
catchError((e) => {
// Doesn't break the stream (tap is called), return the given value to onNext, also call onCompleted.
return [{ error: e }];
// Calls onCompleted, no onNext call.
//return [];
// Rethrow an error, calls onError.
//return throwError('Not recoverable');
}),
//*/
tap(() => {
console.info('Just tapping a bit');
}),
finalize(() => {
console.info('Final step, always done');
}),
).subscribe(
x => console.log('Result:', x),
e => console.error('Error:', e),
() => console.info('Complete'),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment