Files
gpt4free/g4f/errors.py
hlohaus b6f51f00d8 refactor: restructure core utilities, typing, and request handling
- In `g4f/__init__.py`, changed logger setup to use fixed "g4f" name and refactored `ChatCompletion.create` and `create_async` to share `_prepare_request` logic for preprocessing arguments
- In `g4f/config.py`, added `__future__.annotations`, `lru_cache` import, wrapped `get_config_dir` with `@lru_cache`, and simplified platform branch logic
- In `g4f/cookies.py`, added typing imports, renamed `browsers` to `BROWSERS`, reformatted `DOMAINS`, updated docstrings, improved loop logic in `load_cookies_from_browsers` with additional exception handling, split HAR/JSON parsing into `_parse_har_file` and `_parse_json_cookie_file`, and enhanced `read_cookie_files` with optional filters and `.env` loading
- In `g4f/debug.py`, added enable/disable logging functions, updated log handler typing, appended messages to `logs` in `log()`, and improved `error()` formatting
- In `g4f/errors.py`, introduced base `G4FError` and updated all exception classes to inherit from it or relevant subclasses, with descriptive docstrings for each
- In `g4f/files.py`, added `max_length` parameter to `secure_filename`, adjusted regex formatting, and added docstring; updated `get_bucket_dir` to sanitize parts inline with docstring
- In `g4f/typing.py`, added `__future__.annotations`, reorganized imports, restricted PIL import to type-checking, defined `ContentPart` and `Message` TypedDicts, updated type aliases and `__all__` to include new types
- In `g4f/version.py`, added `lru_cache` and request timeout constant, applied caching to `get_pypi_version` and `get_github_version`, added response validation and explicit exceptions, refactored `VersionUtils.current_version` with clearer sources and error on miss, changed `check_version` to return a boolean with optional silent mode, and improved error handling outputs
2025-08-09 03:29:44 +02:00

103 lines
2.2 KiB
Python

class G4FError(Exception):
"""Base exception for all g4f-related errors."""
pass
class ProviderNotFoundError(G4FError):
"""Raised when a provider is not found."""
pass
class ProviderNotWorkingError(G4FError):
"""Raised when the provider is unavailable or failing."""
pass
class StreamNotSupportedError(G4FError):
"""Raised when the requested provider does not support streaming."""
pass
class ModelNotFoundError(G4FError):
"""Raised when a model is not found."""
pass
class ModelNotAllowedError(G4FError):
"""Raised when a model is not allowed by configuration or policy."""
pass
class RetryProviderError(G4FError):
"""Raised to retry with another provider."""
pass
class RetryNoProviderError(G4FError):
"""Raised when there are no providers left to retry."""
pass
class VersionNotFoundError(G4FError):
"""Raised when the version could not be determined."""
pass
class MissingRequirementsError(G4FError):
"""Raised when a required dependency is missing."""
pass
class NestAsyncioError(MissingRequirementsError):
"""Raised when 'nest_asyncio' is missing."""
pass
class MissingAuthError(G4FError):
"""Raised when authentication details are missing."""
pass
class PaymentRequiredError(G4FError):
"""Raised when a provider requires payment before access."""
pass
class NoMediaResponseError(G4FError):
"""Raised when a media request returns no response."""
pass
class ResponseError(G4FError):
"""Base class for response-related errors."""
pass
class ResponseStatusError(ResponseError):
"""Raised when an HTTP response returns a non-success status code."""
pass
class CloudflareError(ResponseStatusError):
"""Raised when a request is blocked by Cloudflare."""
pass
class RateLimitError(ResponseStatusError):
"""Raised when the provider's rate limit has been exceeded."""
pass
class NoValidHarFileError(G4FError):
"""Raised when no valid HAR file is found."""
pass
class TimeoutError(G4FError):
"""Raised for timeout errors during API requests."""
pass
class ConversationLimitError(G4FError):
"""Raised when a conversation limit is reached on the provider."""
pass