From: Matus Ferech Date: Sat, 4 May 2019 15:42:27 +0000 (+0200) Subject: Add `raise_for_status` to `Response` X-Git-Tag: 0.3.0~41^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fb6d14f9e8c3ed2d7f3628906c3a1ac100a58b24;p=thirdparty%2Fhttpx.git Add `raise_for_status` to `Response` --- diff --git a/httpcore/models.py b/httpcore/models.py index c7ed5ae2..97a63cc8 100644 --- a/httpcore/models.py +++ b/httpcore/models.py @@ -14,7 +14,13 @@ from .decoders import ( IdentityDecoder, MultiDecoder, ) -from .exceptions import InvalidURL, ResponseClosed, ResponseNotRead, StreamConsumed +from .exceptions import ( + HttpError, + InvalidURL, + ResponseClosed, + ResponseNotRead, + StreamConsumed, +) from .utils import ( get_reason_phrase, is_known_encoding, @@ -627,3 +633,22 @@ class Response: def __repr__(self) -> str: class_name = self.__class__.__name__ return f"<{class_name}(status_code={self.status_code})>" + + def raise_for_status(self) -> None: + """ + Raise the `HttpError` if one occurred. + """ + message = ( + "{0.status_code} {error_type}: {0.reason_phrase} for url: {0.url}\n" + "For more information check: https://httpstatuses.com/{0.status_code}" + ) + + if 400 <= self.status_code < 500: + message = message.format(self, error_type="Client Error") + elif 500 <= self.status_code < 600: + message = message.format(self, error_type="Server Error") + else: + message = "" + + if message: + raise HttpError(message)