Exceptions¶
All custom exceptions raised by swgoh_comlink inherit from
SwgohComlinkException. Catching this base class is sufficient to handle any
error from the library.
from swgoh_comlink import SwgohComlink
from swgoh_comlink.exceptions import SwgohComlinkException, SwgohComlinkValueError, SwgohComlinkTypeError
comlink = SwgohComlink()
try:
player = comlink.get_player(allycode=123)
except SwgohComlinkValueError:
print("Invalid allycode format")
except SwgohComlinkTypeError:
print("Invalid argument type")
except SwgohComlinkException:
print("Comlink request failed")
HTTP errors¶
When comlink answers with an HTTP error status, the clients raise a
SwgohComlinkHTTPError (or one of its subclasses) from the original
httpx.HTTPStatusError. str(exc) is "HTTP {status}: {body}", as it has
always been, so existing handlers that catch SwgohComlinkException keep
working.
| Exception | Raised for |
|---|---|
SwgohComlinkRateLimitError |
HTTP 429, or the game's Rate exceeded! refusal (sent as HTTP 502) |
SwgohComlinkUnavailableError |
HTTP 503, comlink busy or not ready |
SwgohComlinkClientError |
Any other 4xx, the request was rejected |
SwgohComlinkHTTPError |
Any other error status (5xx) |
Each one has these attributes:
status: the HTTP status code.codeanddetail: thecodeandmessagefields of comlink's JSON error body (detailis the raw body text when the body is not JSON).retry_after: theRetry-Afterheader in seconds, orNone.response: thehttpx.Response.
Game error codes look like GAME_<n>, and the number can differ between calls
that fail for the same reason. Match on the exception class, not on code.
from swgoh_comlink import SwgohComlink
from swgoh_comlink.exceptions import (
SwgohComlinkClientError,
SwgohComlinkHTTPError,
SwgohComlinkRateLimitError,
)
comlink = SwgohComlink()
try:
player = comlink.get_player(allycode=123456789)
except SwgohComlinkRateLimitError as exc:
print(f"Rate limited, retry in {exc.retry_after or 5} s")
except SwgohComlinkClientError as exc:
print(f"Rejected ({exc.status} {exc.code}): {exc.detail}")
except SwgohComlinkHTTPError as exc:
print(f"Comlink error {exc.status}")
To have the client retry rate refusals and 503 responses for you, pass a
RetryPolicy.
API Reference¶
SwgohComlinkException¶
SwgohComlinkException
¶
Bases: Exception
Base class for exceptions in this module.
SwgohComlinkValueError¶
SwgohComlinkValueError
¶
Bases: SwgohComlinkException, ValueError
Raised when an argument value is invalid.
SwgohComlinkTypeError¶
SwgohComlinkTypeError
¶
Bases: SwgohComlinkException, TypeError
Raised when an argument type is invalid.
SwgohComlinkHTTPError¶
SwgohComlinkHTTPError
¶
Bases: SwgohComlinkException
Raised when the comlink service answers with an HTTP error status.
str(exc) is always "HTTP {status}: {body}", and the exception is
raised from the originating httpx.HTTPStatusError, so code that matched
on the message or on SwgohComlinkException keeps working. Use
:meth:from_response to build the most specific subclass for a response.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
The HTTP status code. |
|
code |
The |
|
detail |
The |
|
retry_after |
Seconds the server asked the caller to wait, parsed from the
|
|
response |
The |
from_response(response)
classmethod
¶
Build the most specific HTTP error for an error response.
Classification, first match wins:
- :class:
SwgohComlinkRateLimitError: HTTP 429, aRATE_LIMITEDcode, or a message containing "Rate exceeded" (the game's own refusal, which comlink passes through as HTTP 502). - :class:
SwgohComlinkUnavailableError: HTTP 503. - :class:
SwgohComlinkClientError: any other 4xx status. - :class:
SwgohComlinkHTTPError: everything else (5xx).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The error response returned by |
required |
Returns:
| Type | Description |
|---|---|
SwgohComlinkHTTPError
|
An instance of the matching exception class. It is not raised. |
Examples:
SwgohComlinkRateLimitError¶
SwgohComlinkRateLimitError
¶
Bases: SwgohComlinkHTTPError
Raised when a request was refused for exceeding a rate limit.
Covers HTTP 429 from comlink and the game's own Rate exceeded! refusal,
which arrives as HTTP 502. In both cases the call was not carried out, so
re-issuing it after a pause is safe.
SwgohComlinkUnavailableError¶
SwgohComlinkUnavailableError
¶
Bases: SwgohComlinkHTTPError
Raised on HTTP 503: comlink is busy or not ready and asks to be retried.
comlink usually sends a short Retry-After with this status.
SwgohComlinkClientError¶
SwgohComlinkClientError
¶
Bases: SwgohComlinkHTTPError
Raised on a 4xx status other than 429: the request itself was rejected.
Retrying the same request will not succeed.