Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def has_redirect_location(self) -> bool:
and "Location" in self.headers
)

def raise_for_status(self) -> Response:
def raise_for_status(self, reason: str | None = None) -> Response:
"""
Raise the `HTTPStatusError` if one occurred.
"""
Expand All @@ -807,13 +807,13 @@ def raise_for_status(self) -> Response:

if self.has_redirect_location:
message = (
"{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\n"
"{error_type} '{0.status_code} {0.reason_phrase}{extra_reason}' for url '{0.url}'\n"
"Redirect location: '{0.headers[location]}'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}"
)
else:
message = (
"{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\n"
"{error_type} '{0.status_code} {0.reason_phrase}{extra_reason}' for url '{0.url}'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}"
)

Expand All @@ -825,7 +825,8 @@ def raise_for_status(self) -> Response:
5: "Server error",
}
error_type = error_types.get(status_class, "Invalid status code")
message = message.format(self, error_type=error_type)
extra_reason = f' {reason}' if reason else ''
message = message.format(self, error_type=error_type, extra_reason=extra_reason)
raise HTTPStatusError(message, request=request, response=self)

def json(self, **kwargs: typing.Any) -> typing.Any:
Expand Down
Loading