HTTPError Explained: Python, JavaScript, .NET and Go
An "HTTPError" is an exception your code raises when a server answers with an error status (4xx or 5xx).
The status code in the message tells you what went wrong. The exception itself is just your HTTP library reporting it.
Messages like HTTPError: Not Found, HTTPError: 401, or 404 Client Error: Not Found for url all mean the same thing: the request reached a server, and the server replied with an error status code. To fix it, look up the status code, then check the request your code sent.
Python
requests: requests.exceptions.HTTPError
The requests library does not raise on error statuses by itself. The exception comes from calling raise_for_status(), which raises for any 4xx or 5xx response:
import requests
r = requests.get("https://api.example.com/items/42", timeout=10)
try:
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print(e) # 404 Client Error: Not Found for url: ...
print(e.response.status_code, e.response.text)
urllib: urllib.error.HTTPError
The standard library's urllib.request.urlopen() raises HTTPError automatically for error responses. It is also a response object, so you can read the body the server sent:
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
try:
urlopen("https://example.com/missing")
except HTTPError as e:
print(e.code, e.reason) # 404 Not Found
print(e.read()[:200]) # error body from the server
except URLError as e:
print("Network problem:", e.reason) # DNS, refused connection, TLS...
HTTPError means the server answered; URLError means no HTTP answer was received at all.
JavaScript
fetch()
fetch() only rejects on network failures. A 404 or 500 resolves normally, so you must check response.ok yourself - many codebases then throw their own HttpError:
class HttpError extends Error {
constructor(response) {
super(`HTTP ${response.status} ${response.statusText}`);
this.status = response.status;
}
}
const res = await fetch('/api/items/42');
if (!res.ok) throw new HttpError(res);
Axios
Axios rejects every response outside 2xx by default. The status is in error.response.status; if error.response is undefined, the request never got an answer (network error, CORS, timeout).
Angular: HttpErrorResponse
Angular's HttpClient emits an HttpErrorResponse with status, statusText and error. A status of 0 means no HTTP response was received - usually a CORS rejection, a blocked request, or the server being unreachable.
Node.js: the http-errors package
Express and Koa apps often use http-errors to create errors that carry a status, e.g. createError(404, 'Item not found') or new createError.BadRequest(). An error handler then turns them into HTTP responses.
.NET
HttpClient returns error responses normally; response.EnsureSuccessStatusCode() turns them into an HttpRequestException, which exposes a StatusCode property in .NET 5 and later. Generated clients (such as NSwag) throw an ApiException with messages like "The HTTP status code of the response was not expected (404)".
Go
net/http does not return an error for 4xx/5xx responses - err is only set for transport problems. Always check resp.StatusCode.
How to Fix an HTTPError
- Read the status code and look it up: 400, 401, 403, 404, 429, 500, 502, 503, 504.
- Print the response body. APIs usually explain the problem there (missing field, expired token, unknown ID).
- Check the exact URL and method your code sent - base URL, API version, path parameters, trailing slash.
- Check authentication headers for 401/403 errors.
- Retry only when it makes sense: 429, 502, 503 and 504 are often temporary; 400, 401, 403, 404 and 422 will fail again until the request changes.