Skip to main content

Lib/urllib/error.py

Source:

cpython 3.14 @ ab2d84fe1023/Lib/urllib/error.py

Lib/urllib/error.py defines the three exception classes raised by urllib.request. URLError is the base for all urllib failures. HTTPError is both an exception and a file-like response object. ContentTooShortError signals truncated downloads from urlretrieve.

Map

LinesSymbolRole
1-10importsurllib.response.addinfourl
12-30URLErrorBase exception wrapping a reason string or OSError
32-65HTTPErrorHTTP error code exception that also acts as a response
67-80ContentTooShortErrorTruncated download exception

Reading

URLError

URLError wraps any failure that prevents the URL from being opened. The reason attribute is either a string (for protocol-level errors) or an OSError instance (for network-level errors like ECONNREFUSED).

# CPython: Lib/urllib/error.py:12 URLError
class URLError(OSError):
def __init__(self, reason, filename=None):
self.reason = reason
super().__init__(reason)
if filename is not None:
self.filename = filename

HTTPError: exception and response

HTTPError subclasses both URLError and urllib.response.addinfourl. This dual inheritance means it can be raised as an exception while also providing a file-like read() interface, so callers can read the error body if they catch the exception.

# CPython: Lib/urllib/error.py:32 HTTPError
class HTTPError(URLError, addinfourl):
def __init__(self, url, code, msg, hdrs, fp):
self.code = code
self.msg = msg
self.hdrs = hdrs
self.fp = fp
super().__init__(msg, url)

The code attribute is the HTTP status code (e.g., 404), and hdrs is the response headers. HTTPDefaultErrorHandler in urllib.request raises HTTPError for any 4xx or 5xx response.

ContentTooShortError

ContentTooShortError is raised by urlretrieve when the downloaded file is shorter than the Content-Length header indicates. It stores the partial content in the content attribute.

gopy notes

Not yet ported. The planned package path is module/urllib/. Go's net/http raises errors through the standard error interface; an idiomatic port would define URLError and HTTPError as Go structs implementing error.