Overview
The Grantex Python SDK uses a structured exception hierarchy. All exceptions inherit fromGrantexError, so you can catch all SDK errors with a single handler or handle specific error types individually.
Exception Hierarchy
Import
GrantexError
The base exception for all Grantex SDK errors. Catch this to handle any SDK error:GrantexApiError
Raised when the Grantex API returns a non-2xx HTTP response. Provides access to the HTTP status code, response body, and request ID.Attributes
| Attribute | Type | Description |
|---|---|---|
status_code | int | The HTTP status code (e.g. 404, 422, 500). |
body | Any | The parsed response body (usually a dict) or raw text. |
request_id | str | None | The X-Request-Id header value (if present). |
message or error field, falling back to "HTTP {status_code}".
GrantexAuthError
A subclass ofGrantexApiError raised specifically for 401 (Unauthorized) and 403 (Forbidden) responses. Typically indicates an invalid or expired API key.
GrantexAuthError has the same attributes as GrantexApiError.
GrantexTokenError
Raised when JWT verification or decoding fails. This occurs when usingverify_grant_token() for offline verification or when grants.verify() cannot decode the returned token.
- Token uses an algorithm other than RS256
- Token signature is invalid
- Token is expired
- Required claims are missing (
jti,sub,agt,dev,scp,iat,exp) - Required scopes are not present
- JWKS endpoint is unreachable
- No matching RSA key found in the JWKS
GrantexNetworkError
Raised on network-level failures such as timeouts, DNS resolution errors, or connection refused. The original exception is available via thecause attribute.
Attributes
| Attribute | Type | Description |
|---|---|---|
cause | BaseException | None | The underlying httpx exception that caused the failure. |
Best Practices
Catch Specific Errors First
Order yourexcept clauses from most specific to least specific:
Retry on Transient Errors
Network errors and certain HTTP status codes (429, 502, 503, 504) are often transient and safe to retry:Use Request ID for Support
When reporting issues, include therequest_id from GrantexApiError: