Advanced
Errors
Exceptions raised by the SDK, when they happen and how to handle them.
Every SDK exception inherits from CWAError. Catch CWAError to keep your program running; catch the individual exceptions below to handle each case differently.
import cwa
try:
weather = cwa.weather.get("中正區")
except cwa.AmbiguousLocationError as error:
print("Pick one:", error.candidates)
except cwa.RateLimitError as error:
print(f"Too many requests; retry in {error.retry_after} seconds")
except cwa.CWAError as error:
print("Query failed:", error) Exceptions
| Exception | When | What to do |
|---|---|---|
ConfigurationError | No API key is set, or a client setting is invalid | Check CWA_API_KEY and the client arguments |
AuthenticationError | The API key is invalid or expired | Check your key on the |
RateLimitError | The CWA’s rate limit was exceeded | Retry after retry_after seconds; it is None when the CWA gives no delay |
RequestTimeoutError | A request timed out | Retry later, or raise CWA(timeout=...) |
TransportError | The network connection failed | Check the network and retry |
UpstreamError | The CWA returned another error | Retry later |
DataValidationError | The CWA response is not in the expected format | Usually temporary on the CWA side; retry later, and |
NotFoundError | A dataset, station or township has no data | Check the ID or location |
UnknownLocationError | A place name was not recognized | Check the spelling or use a constant |
AmbiguousLocationError | A place name matches several townships | Pick a full name from candidates |
BatchError | Some locations in a batch failed | See |
MissingDependencyError | A feature needs an extra that is not installed | Install the extra named in the message; see |
ClientClosedError | A client was used after closing | Use the client inside its with block |
An invalid argument, such as days=8, raises Python’s built-in ValueError, which is not a CWAError.
Automatic retries
Network errors, timeouts and temporary CWA errors are retried automatically, up to 3 times by default and for no more than 30 seconds in total; change the count with CWA(retries=...). Invalid keys and invalid arguments would fail again, so they are never retried.