Simulating Errors with RxJS | Task

Ole Ersoy
Feb - 04  -  1 min

Scenario

We want to understand how to go about catching an Observable error, thus we need an Observable that we can subscribe to that will trigger the error.

Approach

Use RxJs throwError:

import { throwError, Observable } from 'rxjs';
const catchMeIfYouCan:Observable<string> = throwError('Catch me if you can')
catchMeIfYouCan.subscribe(
    ()=>console.log("Next handler will not execute"), 
    (e)=>console.error(e), 
    ()=>console.log("Completion handler will not execute"))

Only Catch me if you can is logged, indicating that the Observable errored out.

Demo