Bird
0
0

Why does this Angular HTTP error handler fail?

medium📝 Debug Q7 of 15
Angular - HTTP Client
Why does this Angular HTTP error handler fail?
return this.http.get(url).pipe(
  catchError(() => {
    return throwError('Error occurred');
  })
);

Assuming the caller subscribes and expects an error object.
AcatchError cannot return throwError
BthrowError should receive a function returning the error
CHttpClient.get does not support catchError
DThe error message must be an object, not a string
Step-by-Step Solution
Solution:
  1. Step 1: Check throwError usage

    In RxJS 7+, throwError expects a function returning the error, not the error directly.
  2. Step 2: Identify correct syntax

    The correct usage is throwError(() => new Error('Error occurred')) or similar.
  3. Final Answer:

    throwError should receive a function returning the error -> Option B
  4. Quick Check:

    throwError argument = function returning error [OK]
Quick Trick: Use throwError(() => error) in RxJS 7+ [OK]
Common Mistakes:
MISTAKES
  • Passing error directly to throwError
  • Assuming catchError cannot return throwError
  • Expecting string error instead of Error object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes