Error Handling & Exceptions
Relevant source files
The following files were used as context for generating this wiki page:
The faceit-python library implements a structured exception hierarchy to handle environment issues, authentication failures, and API communication errors. The system categorizes errors into high-level functional groups, allowing you to distinguish between client-side misconfigurations and server-side API failures.
At the core of the system is the FaceitError base class. All custom exceptions within the library inherit from this class, providing a unified catch-all for library-specific issues. The library also integrates with the tenacity package to provide robust retry logic for transient network failures.
Sources: src/faceit/exceptions.py:1-10, src/faceit/http/client.py:64-70
Exception Hierarchy
The library organizes exceptions based on the source of the failure. This includes environment setup, missing credentials, and specific HTTP response codes from the FACEIT API.
Diagram representing the inheritance structure of the library's exception classes.
Sources: src/faceit/exceptions.py:7-78
Environment and Authentication Exceptions
These errors occur before any network request is dispatched to the FACEIT servers.
| Exception Class | Description | Trigger Condition |
|---|---|---|
DecoupleNotFoundError | Signals missing environment management library. | Raised when using FromEnv or env identifiers without python-decouple installed. |
MissingAuthTokenError | Signals a missing API key. | Raised when the specified environment variable key is not found in the environment or .env file. |
Sources: src/faceit/exceptions.py:13-31, src/faceit/http/client.py:161-170
API Communication Exceptions
The APIError class handles failures originating from the FACEIT API. It maps standard HTTP status codes to specific Python exception types using an internal registry.
| HTTP Status | Exception Class | Common Cause |
|---|---|---|
| 400 | BadRequestError | Invalid parameters or malformed request. |
| 401 | UnauthorizedError | Invalid or expired API key. |
| 403 | ForbiddenError | Insufficient permissions for the resource. |
| 404 | NotFoundError | Resource (player, match, team) does not exist. |
| 429 | TooManyRequestsError | Rate limit exceeded. |
| 500 | InternalServerError | Server-side error on FACEIT's end. |
| 503 | ServiceUnavailableError | FACEIT API is temporarily down for maintenance. |
Sources: src/faceit/exceptions.py:72-78
Request Lifecycle and Error Handling
The HTTP client manages the lifecycle of a request, applying retries for transient errors and raising specific exceptions for permanent failures.
Flow of execution showing how the library handles different failure points during an API call.
Sources: src/faceit/http/client.py:173-190, src/faceit/exceptions.py:34-70, tests/test_http_client.py:126-140
Validation and Strict Mode
The BaseResource class performs model validation using Pydantic. Users can configure the behavior when the API returns data that does not match the expected schema via the strict_validation flag.
- Strict Validation Enabled (Default): The library raises a
pydantic.ValidationErrorif the response structure is unexpected. - Strict Validation Disabled: The library logs an exception and issues a
RuntimeWarning, then returns the raw dictionary response instead of crashing.
Sources: src/faceit/api/base.py:108-137
Transient Failures and Retries
The library utilizes tenacity to handle transient network issues automatically.
Default Retry Configuration
By default, the SyncClient and AsyncClient use the following strategy:
- Max Attempts: 3
- Wait Strategy: Random exponential backoff (1 to 10 seconds).
- Retry Conditions:
httpx.TimeoutExceptionhttpx.ConnectErrorhttpx.RemoteProtocolErrorAPIErrorwith status codes 429, 502, 503, or 504.
Sources: src/faceit/http/client.py:84-96, src/faceit/http/helpers.py:38-42
Adaptive Rate Limiting (Async Only)
The AsyncClient implements an adaptive limit to handle SSL/TLS handshake errors, which are common under high concurrency.
- When SSL errors reach a threshold (default 5), the client halves the
max_concurrent_requests. - It monitors a recovery interval (default 300s).
- If the connection remains stable, it gradually increases concurrent capacity back to the initial limit.
Sources: src/faceit/http/client.py:279-335, tests/test_http_client.py:440-475
Pagination Errors
The pagination system validates parameters before execution to prevent common API errors.
- Bad Pagination Request: The library ensures
offsetis a multiple oflimitbefore sending the request to avoid a 400 Bad Request from FACEIT. - Iterator Exhaustion: Setting an
offseton an exhaustedBasePageIteratorraises aValueError. - Method Support: Passing a method that does not support
offset/limitto an iterator raises aValueError.
Sources: src/faceit/api/pagination.py:182-195, src/faceit/api/pagination.py:228-243
The exception system ensures that developers receive actionable information, whether the issue is a missing FACEIT_API_KEY in the environment or a 404 Not Found for a specific player resource.