Skip to Content
cwa
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

ExceptionWhenWhat to do
ConfigurationErrorNo API key is set, or a client setting is invalidCheck CWA_API_KEY and the client arguments
AuthenticationErrorThe API key is invalid or expiredCheck your key on the Open Data platform
RateLimitErrorThe CWA’s rate limit was exceededRetry after retry_after seconds; it is None when the CWA gives no delay
RequestTimeoutErrorA request timed outRetry later, or raise CWA(timeout=...)
TransportErrorThe network connection failedCheck the network and retry
UpstreamErrorThe CWA returned another errorRetry later
DataValidationErrorThe CWA response is not in the expected formatUsually temporary on the CWA side; retry later, and report it if it persists
NotFoundErrorA dataset, station or township has no dataCheck the ID or location
UnknownLocationErrorA place name was not recognizedCheck the spelling or use a constant
AmbiguousLocationErrorA place name matches several townshipsPick a full name from candidates
BatchErrorSome locations in a batch failedSee Partial failures
MissingDependencyErrorA feature needs an extra that is not installedInstall the extra named in the message; see Optional features
ClientClosedErrorA client was used after closingUse 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.