Skip to content

Commit

Permalink
Consolidate to RequestException
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Aug 3, 2024
1 parent f4ed761 commit 72222d9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions curl_cffi/requests/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
__all__ = ["CurlError", "RequestsError", "CookieConflict", "SessionClosed"]

from .. import CurlError

from .exceptions import RequestsError, CookieConflict, SessionClosed
from .exceptions import CookieConflict, SessionClosed
from .exceptions import RequestException as RequestsError
19 changes: 7 additions & 12 deletions curl_cffi/requests/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@
from .. import CurlError


class RequestsError(CurlError, IOError):
"""Base exception for curl_cffi.requests package, alias of RequestException"""
# Note IOError is an alias of OSError in Python 3.x
class RequestException(CurlError, OSError):
"""Base exception for curl_cffi.requests package"""

def __init__(self, msg, code=0, response=None, *args, **kwargs):
super().__init__(msg, code, *args, **kwargs)
self.response = response


class RequestException(RequestsError):
"""Base exception for curl_cffi.requests package."""
pass


class CookieConflict(RequestException):
"""Same cookie exists for different domains."""
pass


class SessionClosed(RequestException):
"""The session has already been closed."""
pass


class ImpersonateError(RequestException):
"""The impersonate config was wrong or impersonate failed."""


class InvalidJSONError(RequestException):
"""A JSON error occurred."""
pass


class JSONDecodeError(InvalidJSONError, json.JSONDecodeError):
"""Couldn't decode the text into json"""
pass


class HTTPError(RequestException):
Expand All @@ -45,7 +41,6 @@ class HTTPError(RequestException):

class ConnectionError(RequestException):
"""A Connection error occurred."""
pass


class ProxyError(RequestException):
Expand Down
8 changes: 4 additions & 4 deletions curl_cffi/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .. import Curl
from .cookies import Cookies
from .errors import RequestsError
from .exceptions import HTTPError, RequestException
from .headers import Headers

CHARSET_RE = re.compile(r"charset=([\w-]+)")
Expand Down Expand Up @@ -138,7 +138,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 RequestsError(f"HTTP Error {self.status_code}: {self.reason}")
raise HTTPError(f"HTTP Error {self.status_code}: {self.reason}")

def iter_lines(self, chunk_size=None, decode_unicode=False, delimiter=None):
"""
Expand Down Expand Up @@ -179,7 +179,7 @@ def iter_content(self, chunk_size=None, decode_unicode=False):
chunk = self.queue.get()

# re-raise the exception if something wrong happened.
if isinstance(chunk, RequestsError):
if isinstance(chunk, RequestException):
self.curl.reset()
raise chunk

Expand Down Expand Up @@ -242,7 +242,7 @@ async def aiter_content(self, chunk_size=None, decode_unicode=False):
chunk = await self.queue.get()

# re-raise the exception if something wrong happened.
if isinstance(chunk, RequestsError):
if isinstance(chunk, RequestException):
await self.aclose()
raise chunk

Expand Down
21 changes: 11 additions & 10 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
from .. import AsyncCurl, Curl, CurlError, CurlHttpVersion, CurlInfo, CurlOpt, CurlSslVersion
from ..curl import CURL_WRITEFUNC_ERROR, CurlMime
from .cookies import Cookies, CookieTypes, CurlMorsel
from .errors import RequestsError, SessionClosed
from .exceptions import ImpersonateError, RequestException, SessionClosed
from .headers import Headers, HeaderTypes
from .impersonate import BrowserType # noqa: F401
from .impersonate import (
TLS_CIPHER_NAME_MAP,
TLS_EC_CURVES_MAP,
TLS_VERSION_MAP,
BrowserType, # noqa: F401
BrowserTypeLiteral,
ExtraFingerprints,
ExtraFpDict,
Expand Down Expand Up @@ -616,7 +616,7 @@ def _set_curl_options(
impersonate = normalize_browser_type(impersonate)
ret = c.impersonate(impersonate, default_headers=default_headers)
if ret != 0:
raise RequestsError(f"Impersonating {impersonate} is not supported")
raise ImpersonateError(f"Impersonating {impersonate} is not supported")

# ja3 string
ja3 = ja3 or self.ja3
Expand Down Expand Up @@ -644,7 +644,8 @@ def _set_curl_options(
extra_fp = ExtraFingerprints(**extra_fp)
if impersonate:
warnings.warn(
"Extra fingerprints was altered after browser version was set.", stacklevel=1
"Extra fingerprints was altered after browser version was set.",
stacklevel=1,
)
self._set_extra_fp(c, extra_fp)

Expand Down Expand Up @@ -982,7 +983,7 @@ def perform():
except CurlError as e:
rsp = self._parse_response(c, buffer, header_buffer, default_encoding)
rsp.request = req
cast(queue.Queue, q).put_nowait(RequestsError(str(e), e.code, rsp))
cast(queue.Queue, q).put_nowait(RequestException(str(e), e.code, rsp))
finally:
if not cast(threading.Event, header_recved).is_set():
cast(threading.Event, header_recved).set()
Expand All @@ -1003,7 +1004,7 @@ def cleanup(fut):

# Raise the exception if something wrong happens when receiving the header.
first_element = _peek_queue(cast(queue.Queue, q))
if isinstance(first_element, RequestsError):
if isinstance(first_element, RequestException):
c.reset()
raise first_element

Expand All @@ -1025,7 +1026,7 @@ def cleanup(fut):
except CurlError as e:
rsp = self._parse_response(c, buffer, header_buffer, default_encoding)
rsp.request = req
raise RequestsError(str(e), e.code, rsp) from e
raise RequestException(str(e), e.code, rsp) from e
else:
rsp = self._parse_response(c, buffer, header_buffer, default_encoding)
rsp.request = req
Expand Down Expand Up @@ -1266,7 +1267,7 @@ async def perform():
except CurlError as e:
rsp = self._parse_response(curl, buffer, header_buffer, default_encoding)
rsp.request = req
cast(asyncio.Queue, q).put_nowait(RequestsError(str(e), e.code, rsp))
cast(asyncio.Queue, q).put_nowait(RequestException(str(e), e.code, rsp))
finally:
if not cast(asyncio.Event, header_recved).is_set():
cast(asyncio.Event, header_recved).set()
Expand All @@ -1287,7 +1288,7 @@ def cleanup(fut):
rsp = self._parse_response(curl, buffer, header_buffer, default_encoding)

first_element = _peek_aio_queue(cast(asyncio.Queue, q))
if isinstance(first_element, RequestsError):
if isinstance(first_element, RequestException):
self.release_curl(curl)
raise first_element

Expand All @@ -1305,7 +1306,7 @@ def cleanup(fut):
except CurlError as e:
rsp = self._parse_response(curl, buffer, header_buffer, default_encoding)
rsp.request = req
raise RequestsError(str(e), e.code, rsp) from e
raise RequestException(str(e), e.code, rsp) from e
else:
rsp = self._parse_response(curl, buffer, header_buffer, default_encoding)
rsp.request = req
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ select = [
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"I", # isort
]

[tool.isort]
profile = "black"
line_length = 100

[tool.mypy]
python_version = "3.8"
ignore_missing_imports = true
Expand Down

0 comments on commit 72222d9

Please sign in to comment.