Bird
0
0

You want to show a friendly message to users when an HTTP error occurs, but still log the original error for debugging. Which code snippet correctly implements this in Angular?

hard🚀 Application Q15 of 15
Angular - HTTP Client
You want to show a friendly message to users when an HTTP error occurs, but still log the original error for debugging. Which code snippet correctly implements this in Angular?
Areturn this.http.get(url).pipe(catchError(err => { console.error(err); return of('Error occurred'); }))
Breturn this.http.get(url).pipe(catchError(err => { return throwError(() => new Error('Error occurred')); }))
Creturn this.http.get(url).pipe(catchError(err => { alert(err.message); return throwError(() => err); }))
Dreturn this.http.get(url).pipe(catchError(err => { alert('Error occurred'); console.error(err); return throwError(() => err); }))
Step-by-Step Solution
Solution:
  1. Step 1: Check user notification and logging

    return this.http.get(url).pipe(catchError(err => { alert('Error occurred'); console.error(err); return throwError(() => err); })) shows an alert to users and logs the error to console, fulfilling both requirements.
  2. Step 2: Verify error propagation

    It returns throwError(() => err) to pass the error downstream for further handling.
  3. Final Answer:

    return this.http.get(url).pipe(catchError(err => { alert('Error occurred'); console.error(err); return throwError(() => err); })) -> Option D
  4. Quick Check:

    Alert user + log error + rethrow = C [OK]
Quick Trick: Alert user, log error, then rethrow with throwError(() => err) [OK]
Common Mistakes:
MISTAKES
  • Not alerting user or not logging error
  • Returning a string instead of throwing error
  • Alerting raw error message confusing users

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes