Skip to content

Commit

Permalink
Add response attribute to raise_for_status errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lexiforest committed Dec 29, 2024
1 parent 0563870 commit 3068b48
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion curl_cffi/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _decode(self, content: bytes) -> str:
def raise_for_status(self):
"""Raise an error if status code is not in [200, 400)"""
if not self.ok:
raise HTTPError(f"HTTP Error {self.status_code}: {self.reason}")
raise HTTPError(f"HTTP Error {self.status_code}: {self.reason}", 0, self)

def iter_lines(self, chunk_size=None, decode_unicode=False, delimiter=None):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/unittest/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from curl_cffi import CurlOpt, requests
from curl_cffi.const import CurlECode, CurlInfo
from curl_cffi.requests.errors import SessionClosed
from curl_cffi.requests.exceptions import HTTPError
from curl_cffi.requests.models import Response


Expand Down Expand Up @@ -403,6 +404,15 @@ def test_reason(server):
assert r.reason == "OK"


def test_raise_for_status(server):
r = requests.get(str(server.url.copy_with(path="/status/400")))
assert r.status_code == 400
try:
r.raise_for_status()
except HTTPError as e:
assert e.response.status_code == 400 # type: ignore


#######################################################################################
# testing session
#######################################################################################
Expand Down

0 comments on commit 3068b48

Please sign in to comment.