Skip to main content

Error handling

HTTP operations raise HTTPError, which carries kind, message, and optional method, url, and status_code context. Not every error populates every field.

import req


def main() raises:
try:
var response = req.get("https://example.com")
response.raise_for_status()
except error:
if error.kind == req.ErrorKind.HTTPStatusError:
print(error.status_code.value())
else:
print(error.kind, error.message)

HTTP 4xx/5xx is a normal response until raise_for_status() is called. Transport failures raise during sending or body consumption. A successful header response does not guarantee that a streamed body can be read completely.

KindsMeaning
InvalidURL, InvalidRequestInvalid URL, headers, body, timeout, or configuration.
ConnectError, ReadError, WriteErrorTransport failure in the corresponding phase.
ConnectTimeout, ReadTimeout, WriteTimeoutPhase timeout.
TLSError, ProtocolErrorTLS verification/handshake or HTTP protocol failure.
TooManyRedirects, UnsafeRedirectRedirect limit or HTTPS downgrade.
HTTPStatusErrorExplicitly raised HTTP 4xx/5xx.
DecodeError, JSONDecodeErrorText/JSON decoding or invalid typed JSON access.
ClientClosed, StreamClosedOperation on a closed client or stream.
StreamNotRead, StreamConsumedWhole-body access before buffering or after chunk consumption.

Req does not automatically retry requests. If your application retries, consider whether the operation and request body can safely be sent again.

API details​

HTTPError / ErrorKind