Bird
0
0

Which of the following is the correct syntax to handle HTTP errors using catchError in an Angular service?

easy📝 Syntax Q3 of 15
Angular - HTTP Client
Which of the following is the correct syntax to handle HTTP errors using catchError in an Angular service?
Areturn this.http.get(url).pipe(catchError(error => throwError(() => error)));
Breturn this.http.get(url).catchError(error => console.log(error));
Creturn this.http.get(url).subscribe(catchError(error => error));
Dreturn this.http.get(url).pipe(map(error => catchError(error)));
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct RxJS pipe usage

    Errors are handled inside the pipe() method using catchError operator.
  2. Step 2: Check error handling function

    The function inside catchError should return an observable, often using throwError to propagate the error.
  3. Final Answer:

    return this.http.get(url).pipe(catchError(error => throwError(() => error))); -> Option A
  4. Quick Check:

    Correct catchError syntax = pipe + catchError + throwError [OK]
Quick Trick: Use pipe() with catchError and throwError for error handling [OK]
Common Mistakes:
MISTAKES
  • Using catchError outside pipe
  • Calling catchError directly on observable
  • Misusing subscribe with catchError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes