This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: HTTPException is derived from Exception instead of IOError
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Pastafarianist, martin.panter
Priority: normal Keywords:

Created on 2015-08-04 20:10 by Pastafarianist, last changed 2022-04-11 14:58 by admin.

Messages (5)
msg247995 - (view) Author: Pastafarianist (Pastafarianist) Date: 2015-08-04 20:10
In both Python 2 and Python 3, HTTPException is derived from Exception. This is not quite convenient, since catching all connection-related errors while performing an HTTP query requires catching both IOError (which is subclassed by socket.error) and HTTPException. It might be better to change the parent class to IOError instead.
msg248009 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-08-04 23:51
Did you have any specific exceptions in mind, or specific subclasses of IOError? In 3.5 we now have the RemoteDisconnected exception, which derives ConnectionResetError. Some of the other exceptions are only local programmer errors. Currently documented exceptions:

Exception:
  http.client.HTTPException:
  * NotConnected
  * InvalidURL
  * UnknownProtocol
  * UnknownTransferEncoding
  * UnimplementedFileMode
  * IncompleteRead
  * ImproperConnectionState:
    + CannotSendRequest
    + CannotSendHeader
    + ResponseNotReady
  * BadStatusLine
    + RemoteDisconnected
  * LineTooLong

NotConnected is a programmer error, and it is only raised if the undocumented and untested “auto_open” flag is cleared.

InvalidURL is triggered by the input URL, not the network or remote server.

UnknownTransferEncoding was once triggered by the remote response, but is no longer invoked; see Issue 600488. Maybe it should be deprecated?

UnimplementedFileMode looks like it was once either an internal consistency error, or a programmer error, raised by passing the wrong mode to FakeSocket.makefile(). Unused since r57680; probably should also be deprecated.

The ImproperConnectionState hierarchy exceptions are also programmer errors, raised when the wrong method is called according to the local object state.

I would perhaps support UnknownProtocol, IncompleteRead, BadStatusLine, LineTooLong (and maybe UnknownTransferEncoding) being derived from some more specific exception. Maybe ValueError would be more appropriate for protocol errors, EOFError for IncompleteRead. I’m not sure.

OSError is specifically designed for errors related to C’s “errno”, and none of these errors are related to “errno”. Though I acknowledge there are other subclasses that ignore this fact, such as URLError.
msg249167 - (view) Author: Pastafarianist (Pastafarianist) Date: 2015-08-25 23:20
I agree that the five exception types you mentioned (UnknownProtocol, UnknownTransferEncoding, IncompleteRead, BadStatusLine, LineTooLong) should be derived from IOError. EOFError is probably not a good choice here. It's not something I would expect to see in networking code. ValueError may be better, but I don't know enough about the library to tell whether it is common to use ValueError in similar contexts.

However, ImproperConnectionState (specifically, CannotSendRequest) may be not a programmer error, and, thus, it might be better to have it derived from IOError. See https://hg.python.org/cpython/file/3.5/Lib/http/client.py#l933. Depending on your point of view, you may consider this to be a programmer error or not.
msg249168 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-08-25 23:59
Regarding CannotSendRequest: HTTPConnection will connect again with a new socket if a previous response will close the old connection. See a few lines below where you linked: “If point (2) is true, then we . . . will open a new [socket] when a new request is made.”
msg249219 - (view) Author: Pastafarianist (Pastafarianist) Date: 2015-08-26 21:27
My bad then.

How do we proceed with introducing a change to the standard library?
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68976
2015-08-26 21:27:04Pastafarianistsetmessages: + msg249219
2015-08-25 23:59:16martin.pantersetmessages: + msg249168
2015-08-25 23:20:13Pastafarianistsetmessages: + msg249167
2015-08-04 23:51:30martin.pantersetnosy: + martin.panter
messages: + msg248009
2015-08-04 20:10:42Pastafarianistcreate