Catcherror In Angular
The RxJs operator Angular CatchError is used in Angular. It can be used to handle the errors that the Angular Observable throws. The CatchError, like all other RxJs operators, accepts an observable as input and returns an observable (or throws an error). We can use CatchError to throw a user-defined error or give a substitute observable. Let's have a look at all of them in this article.
Handling Errors in Observable
We have two options for dealing with mistakes.
- Using the subscribe method's error callback
- Errors in the observable stream should be Catch.
Using The Error Callback of Subscribe method
The subscribe method is used to subscribe to an Observable. As parameters, the subscribe method accepts three callback methods. The next value, error, or complete event is what they are. The error callback is used to catch and manage errors.
Consider the following code as an example. The map operator is used by the obs observable to multiply the values (srcArray) by two. If the result is NaN, we throw an error with the throw new Error function ("Result is NaN").
srcArray = from([1, 2, 'A', 4]); obs = this.srcArray .pipe( map(val => { let result = val as number * 2; if (Number.isNaN(result)) { console.log('Errors Occurred in Stream') throw new Error("Result is NaN") } return result }), ); ngOnInit() { this.obs.subscribe( el => { console.log('Value Received ' + el) }, err => { console.log("Error caught at Subscriber " + err) }, () => console.log("Processing Complete.") ) } **** Output ***** Value Received 2 Value Received 4 Errors Occurred in Stream Error Caught at subscriber Error: Result is NaN
Catch errors in the observable stream
Using CatchError Operator
import { catchError } from 'rxjs/operators'
Syntax
Returning a new observable
srcArray = from([1, 2, 'A', 4]); obs = this.srcArray .pipe( map(val => { let result = val as number * 2; if (Number.isNaN(result)) { console.log('Errors Occurred in Stream') throw new Error("Result is NaN") } return result }), catchError(error => { console.log('Caught in CatchError. Returning 0') return of(0); //return from(['A','B','C']) }) ); //Output Value Received 2 Value Received 4 Errors Occurred in Stream Caught in CatchError. Returning 0 Value Received 0 Observable Completed
Throws a new Error
obs = this.srcArray .pipe( map(val => { let result = val as number * 2; if (Number.isNaN(result)) { console.log('Errors Occurred in Stream') throw new Error("Result is NaN") } return result }), catchError(error => { console.log('Caught in CatchError. Throwing error') throw new Error(error) }) ); //OUTPUT Value Received 2 Value Received 4 Errors Occurred in Stream Caught in CatchError. Throwing error Error caught at Subscriber Error: Error: Result is NaN
ThrowError can also be used to return an observable. Remember that throwError produces an observable that emits an error immediately rather than throwing an error like throw new Error.
obs = this.srcArray .pipe( map(val => { let result = val as number * 2; if (Number.isNaN(result)) { console.log('Errors Occurred in Stream') throw new Error("Result is NaN") } return result }), catchError(error => { console.log('Caught in CatchError. Throwing error') return throwError(error); }) ); //OUTPUT Value Received 2 Value Received 4 Errors Occurred in Stream Caught in CatchError. Throwing error Error caught at Subscriber Error: Result is NaN
Retrying
obs = this.srcArray .pipe( map(val => { let result = val as number * 2; if (Number.isNaN(result)) { console.log('Errors Occurred in Stream') throw new Error("Result is NaN") } return result }), retry(2), catchError((error,src) => { console.log('Caught in CatchError. Throwing error') throw new Error(error) }) ); //Output Value Received 2 Value Received 4 Errors Occurred in Stream Value Received 2 Value Received 4 Errors Occurred in Stream Value Received 2 Value Received 4 Errors Occurred in Stream Caught in CatchError. Throwing error Error caught at Subscriber Error: Error: Result is NaN
obs = this.srcArray .pipe( map(val => { let result = val as number * 2; if (Number.isNaN(result)) { console.log('Errors Occurred in Stream') throw new Error("Result is NaN") } return result }), catchError((error,src) => { console.log('Caught in CatchError. Throwing error') this.count++; if (this.count < 2) { return src; } else { throw new Error(error) } }) );