Angular FAQ: Top Questions
25. How to handle errors in Angular HTTP requests?
Error handling in HTTP is done using RxJS operators like catchError. It allows you to gracefully handle failed requests.
this.http.get('/api/data')
.pipe(
catchError(error => {
this.errorMsg = error.message;
return throwError(() => error);
})
).subscribe();
- Import
catchErrorandthrowErrorfromrxjs. - Display fallback UI or log errors as needed.
