Sub topic 1
HttpClient requests and where they can fail
Prerequisites
This chapter assumes TypeScript fundamentals, Angular services, RxJS Observables, and basic use of Angular HttpClient.
HttpClient requests and where they can fail
HttpClient sends HTTP requests and returns an Observable. The request usually starts only when code subscribes to that Observable. A request can fail before a response arrives, or the server can return an HTTP error status.
Reading an HttpErrorResponse
Angular reports failed HTTP requests with HttpErrorResponse. It includes properties such as:
status: the numeric HTTP status. A value of0commonly means that no usable response arrived.statusText: a short status description.url: the requested URL.error: response details, which might be JSON, text, or a browser error.message: Angular’s description of the failure.
The HttpErrorResponse is emitted through the Observable error channel, so normal success handling does not receive it.
Network errors, 4xx and 5xx responses
A network error can result from a lost connection, DNS failure, blocked request, or CORS configuration. It often has status 0.
A 4xx response means the request could not be fulfilled because of the client or request. Common examples include 401 Unauthorized, 403 Forbidden, and 404 Not Found.
A 5xx response means the server failed while processing a valid-looking request. These errors may be temporary. The status category matters when deciding whether to show an error, refresh authentication, or retry later.
| Failure | Typical status | Usual handling |
|---|---|---|
| Network failure | `0` | Check connection or report unavailable service |
| Client error | `400–499` | Fix input, permissions, or authentication |
| Server error | `500–599` | Report temporary failure or consider retrying |
> callout note > Do not treat every error as retryable. Repeating a rejected POST request could create duplicate data.
Handling errors with `catchError`
RxJS catchError intercepts an error and must return another Observable. The following service converts Angular’s error into a simpler Error for its callers.
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable, catchError, throwError } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class UserService {
private http = inject(HttpClient);
getUser(id: number): Observable<unknown> {
return this.http.get(`/api/users/${id}`).pipe(
catchError((error: HttpErrorResponse) => {
const message =
error.status === 0
? 'Network error. Check your connection.'
: `Server returned ${error.status}`;
return throwError(() => new Error(message));
}),
);
}
}throwError keeps the stream failed, allowing a component or another service to decide how to display the failure.
Exercise
What does status 0 usually indicate, and how does catchError preserve the failure?
Show answer
Status 0 usually indicates a network failure. catchError can transform the error and return throwError so the caller still receives an error.