Sub topic 3
Network errors, 4xx and 5xx responses
This chapter assumes you can make requests with HttpClient, use pipe() on Observables, and read the fields of an HttpErrorResponse. Here you will sort failures into three categories. The categories matter because each one calls for a different reaction.
Why categories matter
Every failed HttpClient request arrives as an HttpErrorResponse, so the type alone does not say what went wrong. The status field does. It lets you decide whether to show a message, ask the user to fix something, or try again.
| Category | `status` | Typical cause | Usually worth retrying? |
|---|---|---|---|
| Network error | `0` | No connection, DNS failure, CORS block, aborted request | Sometimes |
| Client error | `400` to `499` | Bad input, missing login, unknown resource | No, except `408` and `429` |
| Server error | `500` to `599` | Crash or overload on the server | Sometimes: `500` rarely, `502`, `503` and `504` often |
Network errors
A status of 0 means the browser never received an HTTP response. The server may be unreachable, the user may be offline, or the browser may have blocked the call. In this case error.error is usually a browser ProgressEvent or ErrorEvent, not data from your API. Do not try to read a message from it.
Client errors (4xx)
A 4xx status means the server understood the request and refused it. Repeating the same request will normally give the same answer.
400and422: the data was invalid. Show field errors to the user.401: the user is not authenticated. A later chapter covers refreshing credentials.403: the user is authenticated but not allowed.404: the resource does not exist.408and429: timeout and rate limiting. These are the 4xx codes that a retry can fix.
Server errors (5xx)
A 5xx status means the request may have been fine but the server failed. 502, 503 and 504 often point to a gateway or overload problem that passes quickly. A plain 500 is often a bug that a retry will not fix.
Classifying in code
A small helper keeps this decision in one place:
import { HttpErrorResponse } from '@angular/common/http';
export type ErrorCategory = 'network' | 'client' | 'server' | 'unknown';
export function categorize(error: HttpErrorResponse): ErrorCategory {
if (error.status === 0) {
return 'network';
}
if (error.status >= 400 && error.status < 500) {
return 'client';
}
if (error.status >= 500 && error.status < 600) {
return 'server';
}
return 'unknown';
}The unknown branch is not dead code. If the server answers 200 but the body cannot be parsed, for example when you expect JSON and get HTML, HttpClient still emits an HttpErrorResponse with a 2xx status.
The decision flow
Common mistakes
- Treating all errors as server errors. A
404is not an outage, and showing "server down" confuses users. - Reading
error.error.messagewithout checking the status. For status0that property may not exist. - Retrying every failure. A
400with the same body will fail again, and retrying a429with no delay makes the rate limiting worse.
In the next chapter you will put categorize to work inside catchError, which turns each category into a meaningful error for callers.
Exercise
A request fails and error.status is 503. Which category is it, and is a retry reasonable? What if the status were 404?
Show answer
A 503 is a server error (5xx). It often signals temporary overload, so a retry with a delay is reasonable. A 404 is a client error (4xx). The resource does not exist, so repeating the same request will fail the same way and should not be retried.