From 9b8f5af7596ab2208375a4d26b5b585d51b82b01 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 21 Apr 2021 10:51:35 +0100 Subject: [PATCH] `httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584) * ResponseClosed -> StreamClosed * Update docs for StreamClosed --- docs/exceptions.md | 6 +++--- httpx/__init__.py | 4 ++-- httpx/_exceptions.py | 31 +++++++++++++++---------------- httpx/_models.py | 6 +++--- tests/models/test_responses.py | 4 ++-- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/docs/exceptions.md b/docs/exceptions.md index 949ac47a..3de8fc6b 100644 --- a/docs/exceptions.md +++ b/docs/exceptions.md @@ -162,11 +162,11 @@ except httpx.HTTPStatusError as exc: ::: httpx.StreamConsumed :docstring: +::: httpx.StreamClosed + :docstring: + ::: httpx.ResponseNotRead :docstring: ::: httpx.RequestNotRead :docstring: - -::: httpx.ResponseClosed - :docstring: diff --git a/httpx/__init__.py b/httpx/__init__.py index af38f8a9..7b130937 100644 --- a/httpx/__init__.py +++ b/httpx/__init__.py @@ -23,8 +23,8 @@ from ._exceptions import ( RemoteProtocolError, RequestError, RequestNotRead, - ResponseClosed, ResponseNotRead, + StreamClosed, StreamConsumed, StreamError, TimeoutException, @@ -99,10 +99,10 @@ __all__ = [ "RequestError", "RequestNotRead", "Response", - "ResponseClosed", "ResponseNotRead", "StatusCode", "stream", + "StreamClosed", "StreamConsumed", "StreamError", "SyncByteStream", diff --git a/httpx/_exceptions.py b/httpx/_exceptions.py index 89e6fcb7..b6e59aa0 100644 --- a/httpx/_exceptions.py +++ b/httpx/_exceptions.py @@ -27,9 +27,9 @@ Our exception hierarchy: * CookieConflict * StreamError x StreamConsumed + x StreamClosed x ResponseNotRead x RequestNotRead - x ResponseClosed """ import contextlib import typing @@ -262,7 +262,7 @@ class CookieConflict(Exception): # the request/response stream in an invalid manner. -class StreamError(Exception): +class StreamError(RuntimeError): """ The base class for stream exceptions. @@ -292,6 +292,19 @@ class StreamConsumed(StreamError): super().__init__(message) +class StreamClosed(StreamError): + """ + Attempted to read or stream response content, but the request has been + closed. + """ + + def __init__(self) -> None: + message = ( + "Attempted to read or stream content, but the stream has " "been closed." + ) + super().__init__(message) + + class ResponseNotRead(StreamError): """ Attempted to access streaming response content, without having called `read()`. @@ -312,20 +325,6 @@ class RequestNotRead(StreamError): super().__init__(message) -class ResponseClosed(StreamError): - """ - Attempted to read or stream response content, but the request has been - closed. - """ - - def __init__(self) -> None: - message = ( - "Attempted to read or stream response content, but the request has " - "been closed." - ) - super().__init__(message) - - @contextlib.contextmanager def request_context(request: "Request" = None) -> typing.Iterator[None]: """ diff --git a/httpx/_models.py b/httpx/_models.py index 1bb0d7fc..2e4a3b6c 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -27,8 +27,8 @@ from ._exceptions import ( HTTPStatusError, InvalidURL, RequestNotRead, - ResponseClosed, ResponseNotRead, + StreamClosed, StreamConsumed, request_context, ) @@ -1222,7 +1222,7 @@ class Response: if self.is_stream_consumed: raise StreamConsumed() if self.is_closed: - raise ResponseClosed() + raise StreamClosed() if not isinstance(self.stream, SyncByteStream): raise RuntimeError("Attempted to call a sync iterator on an async stream.") @@ -1320,7 +1320,7 @@ class Response: if self.is_stream_consumed: raise StreamConsumed() if self.is_closed: - raise ResponseClosed() + raise StreamClosed() if not isinstance(self.stream, AsyncByteStream): raise RuntimeError("Attempted to call an async iterator on an sync stream.") diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py index 10a3f1aa..78f5db0b 100644 --- a/tests/models/test_responses.py +++ b/tests/models/test_responses.py @@ -660,7 +660,7 @@ def test_cannot_read_after_response_closed(): ) response.close() - with pytest.raises(httpx.ResponseClosed): + with pytest.raises(httpx.StreamClosed): response.read() @@ -672,7 +672,7 @@ async def test_cannot_aread_after_response_closed(): ) await response.aclose() - with pytest.raises(httpx.ResponseClosed): + with pytest.raises(httpx.StreamClosed): await response.aread()