An HTTP response code communicates what happened while processing a request. Servers should return meaningful error codes and end failed handlers early; clients can inspect the numeric code or status text. section 8.3 section 11.2
Interpret response-code ranges before deciding how to handle a response:
- Treat 20x responses as successful requests. section 11.2
- Interpret 30x responses as redirects, 400-range responses as client errors, and 500-range responses as server errors. section 11.2.2
- Use 401 or 403 responses to trigger authentication or reauthentication, and consider incremental backoff for 429 or 500 responses. section 11.2 section 11.2.2
- Prefer Go constants such as http.StatusForbidden over unexplained numeric literals. section 8.3 section 11.2
Return and convert an HTTP error
A Go handler can return a plain-text 403 with http.Error(w, "An Error Occurred", http.StatusForbidden). A client can inspect res.StatusCode and Content-Type, then convert an unexpected non-2xx response into a custom error carrying the HTTP code, application code, and message.










