0
0
Angularframework~5 mins

catchError for error handling in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of catchError in Angular?

catchError is used to handle errors in observable streams. It lets you catch errors and return a new observable or perform side effects, preventing the app from crashing.

Click to reveal answer
beginner
How do you import <code>catchError</code> in an Angular service?
<p>You import it from <code>rxjs/operators</code> like this:<br><code>import { catchError } from 'rxjs/operators';</code></p>
Click to reveal answer
intermediate
What does the catchError operator expect as its argument?

It expects a function that takes an error and returns a new observable, often using of() to emit a fallback value or throwError() to rethrow.

Click to reveal answer
beginner
Show a simple example of using catchError in an HTTP request.
this.http.get('/api/data').pipe(
  catchError(error => {
    console.error('Error caught:', error);
    return of([]); // Return empty array as fallback
  })
);
Click to reveal answer
beginner
Why is it important to handle errors with catchError in Angular apps?

Handling errors prevents the app from crashing, improves user experience by showing friendly messages or fallback data, and helps developers debug issues.

Click to reveal answer
What does catchError return when it catches an error?
AA plain value
BA promise
CA new observable
DAn error object
Where do you import catchError from in Angular?
A'rxjs/operators'
B'@angular/core'
C'rxjs'
D'@angular/common/http'
Which RxJS function is commonly used inside catchError to return fallback data?
Aof()
Bmap()
Cfilter()
DthrowError()
What happens if you don't use catchError in an observable that errors?
AThe observable will ignore the error
BThe error will crash the app or stop the stream
CThe error will be logged automatically
DThe observable will retry automatically
Which of these is a correct way to use catchError?
Aobservable.catchError(error => throw error)
BcatchError(observable => error)
CcatchError(() => console.log('error'))
Dobservable.pipe(catchError(error => of('fallback')))
Explain how catchError helps in managing errors in Angular observable streams.
Think about what happens when an error occurs in a data stream.
You got /4 concepts.
    Describe a simple example of using catchError with an HTTP request in Angular.
    Imagine you want to show empty data if the server fails.
    You got /4 concepts.