Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement requests-like exception hierarchy, close #201 #250

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
18 changes: 4 additions & 14 deletions curl_cffi/requests/errors.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
from .. import CurlError


class RequestsError(CurlError):
"""Base exception for curl_cffi.requests package"""
# for compatibility with 0.5.x

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


class CookieConflict(RequestsError):
pass
__all__ = ["CurlError", "RequestsError", "CookieConflict", "SessionClosed"]

from .. import CurlError

class SessionClosed(RequestsError):
pass
from .exceptions import RequestsError, CookieConflict, SessionClosed
135 changes: 133 additions & 2 deletions curl_cffi/requests/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,134 @@
from .errors import RequestsError
# Apache 2.0 License
# Vendored from https://github.com/psf/requests/blob/main/src/requests/exceptions.py

RequestsException = RequestsError
import json

from .. import CurlError


class RequestsError(CurlError, IOError):
"""Base exception for curl_cffi.requests package, alias of RequestException"""

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 InvalidJSONError(RequestException):
"""A JSON error occurred."""
pass


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


class HTTPError(RequestException):
"""An HTTP error occurred."""


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


class ProxyError(RequestException):
"""A proxy error occurred."""


class SSLError(ConnectionError):
"""An SSL error occurred."""


class Timeout(RequestException):
"""The request timed out."""


class ConnectTimeout(ConnectionError, Timeout):
"""The request timed out while trying to connect to the remote server.

Requests that produced this error are safe to retry.
"""


class ReadTimeout(Timeout):
"""The server did not send any data in the allotted amount of time."""


class URLRequired(RequestException):
"""A valid URL is required to make a request."""


class TooManyRedirects(RequestException):
"""Too many redirects."""


class MissingSchema(RequestException, ValueError):
"""The URL scheme (e.g. http or https) is missing."""


class InvalidSchema(RequestException, ValueError):
"""The URL scheme provided is either invalid or unsupported."""


class InvalidURL(RequestException, ValueError):
"""The URL provided was somehow invalid."""


class InvalidHeader(RequestException, ValueError):
"""The header value provided was somehow invalid."""


class InvalidProxyURL(InvalidURL):
"""The proxy URL provided is invalid."""


class ChunkedEncodingError(RequestException):
"""The server declared chunked encoding but sent an invalid chunk."""


class ContentDecodingError(RequestException):
"""Failed to decode response content."""


class StreamConsumedError(RequestException, TypeError):
"""The content for this response was already consumed."""


class RetryError(RequestException):
"""Custom retries logic failed"""


class UnrewindableBodyError(RequestException):
"""Requests encountered an error when trying to rewind a body."""


# Warnings


class RequestsWarning(Warning):
"""Base warning for Requests."""


class FileModeWarning(RequestsWarning, DeprecationWarning):
"""A file was opened in text mode, but Requests determined its binary length."""


class RequestsDependencyWarning(RequestsWarning):
"""An imported dependency doesn't match the expected version range."""
10 changes: 5 additions & 5 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .errors import RequestsError, SessionClosed
from .headers import Headers, HeaderTypes
from .models import Request, Response
from .websockets import WebSocket
from .websockets import ON_CLOSE_T, ON_ERROR_T, ON_MESSAGE_T, ON_OPEN_T, WebSocket

try:
import gevent
Expand Down Expand Up @@ -666,10 +666,10 @@ def ws_connect(
self,
url,
*args,
on_message: Optional[Callable[[WebSocket, str], None]] = None,
on_error: Optional[Callable[[WebSocket, str], None]] = None,
on_open: Optional[Callable] = None,
on_close: Optional[Callable] = None,
on_message: Optional[ON_MESSAGE_T] = None,
on_error: Optional[ON_ERROR_T] = None,
on_open: Optional[ON_OPEN_T] = None,
on_close: Optional[ON_CLOSE_T] = None,
**kwargs,
):
self._check_session_closed()
Expand Down
4 changes: 2 additions & 2 deletions curl_cffi/requests/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from enum import IntEnum
from typing import Callable, Optional, Tuple

from curl_cffi.const import CurlECode, CurlWsFlag
from curl_cffi.curl import CurlError
from ..const import CurlECode, CurlWsFlag
from ..curl import CurlError


ON_MESSAGE_T = Callable[["WebSocket", bytes], None]
Expand Down
Loading