Skip to content

Exceptions

All exceptions raised by PyiCloud derive from PyiCloudException, so a single except PyiCloudException block catches everything the library raises.

Library exceptions.

PyiCloudException

Bases: Exception

Generic iCloud exception.

Source code in pyicloud/exceptions.py
12
13
class PyiCloudException(Exception):
    """Generic iCloud exception."""

PyiCloudPasswordException

Bases: PyiCloudException

Password exception.

Source code in pyicloud/exceptions.py
16
17
class PyiCloudPasswordException(PyiCloudException):
    """Password exception."""

PyiCloudServiceUnavailable

Bases: PyiCloudException

Service unavailable exception.

Source code in pyicloud/exceptions.py
20
21
class PyiCloudServiceUnavailable(PyiCloudException):
    """Service unavailable exception."""

TokenException

Bases: PyiCloudException

Token exception.

Source code in pyicloud/exceptions.py
24
25
class TokenException(PyiCloudException):
    """Token exception."""

PyiCloudAPIResponseException

Bases: PyiCloudException

iCloud response exception.

Source code in pyicloud/exceptions.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class PyiCloudAPIResponseException(PyiCloudException):
    """iCloud response exception."""

    def __init__(
        self,
        reason: str,
        code: int | str | None = None,
        response: Response | None = None,
    ) -> None:
        """Capture a normalized API error and the optional HTTP context."""
        self.reason: str = reason
        self.code: int | str | None = code
        self.response: Response | None = response
        message: str = reason or ""
        if code:
            message += f" ({code})"

        if response is not None and response.text:
            message += f": {response.text}"

        super().__init__(message)

PyiCloudServiceNotActivatedException

Bases: PyiCloudAPIResponseException

iCloud service not activated exception.

Source code in pyicloud/exceptions.py
52
53
class PyiCloudServiceNotActivatedException(PyiCloudAPIResponseException):
    """iCloud service not activated exception."""

PyiCloudPCSTimeoutException

Bases: PyiCloudAPIResponseException

Raised when PCS access could not be granted after all retries.

Source code in pyicloud/exceptions.py
56
57
class PyiCloudPCSTimeoutException(PyiCloudAPIResponseException):
    """Raised when PCS access could not be granted after all retries."""

PyiCloudConnectionException

Bases: PyiCloudAPIResponseException

Raised when iCloud could not be reached at all.

A connection error, DNS failure or timeout says nothing about whether the caller's credentials or session are still good, only that no answer came back. Callers need to tell the two apart because the remedies are opposite: a rejected password is fixed by asking the user to log in again, while an unreachable service is fixed by waiting and retrying.

Distinguishing it matters most during authentication, where every transport failure used to be reported as a failed login. A consumer acting on that asks the user to re-enter working credentials in the middle of an outage, and, if it stops polling until they do, stays down long after iCloud has come back.

Subclasses the generic response error so existing handlers keep catching it, and carries no code because no HTTP response was received.

Source code in pyicloud/exceptions.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class PyiCloudConnectionException(PyiCloudAPIResponseException):
    """Raised when iCloud could not be reached at all.

    A connection error, DNS failure or timeout says nothing about whether the
    caller's credentials or session are still good, only that no answer came
    back. Callers need to tell the two apart because the remedies are opposite:
    a rejected password is fixed by asking the user to log in again, while an
    unreachable service is fixed by waiting and retrying.

    Distinguishing it matters most during authentication, where every transport
    failure used to be reported as a failed login. A consumer acting on that
    asks the user to re-enter working credentials in the middle of an outage,
    and, if it stops polling until they do, stays down long after iCloud has
    come back.

    Subclasses the generic response error so existing handlers keep catching
    it, and carries no ``code`` because no HTTP response was received.
    """

PyiCloudEndpointGoneException

Bases: PyiCloudAPIResponseException

Raised when Apple reports an endpoint as permanently gone (HTTP 410).

Apple withdraws endpoints without notice, and has done so more than once. A 410 means the endpoint this library calls no longer exists, so it signals that pyicloud needs updating rather than that the caller's request, credentials, or session were wrong.

Distinguishing it matters because the alternative is expensive: when the Photos upload endpoint was withdrawn it surfaced as a generic response error, and the reporter ruled out a stale session, an outdated client, a missing PCS handshake, the wrong dsid, and the wrong partition before concluding the endpoint itself had gone.

endpoint is a redacted host-and-path label safe to quote in a bug report; the full URL and the response body are available on response when one was captured.

Source code in pyicloud/exceptions.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class PyiCloudEndpointGoneException(PyiCloudAPIResponseException):
    """Raised when Apple reports an endpoint as permanently gone (HTTP 410).

    Apple withdraws endpoints without notice, and has done so more than once.
    A 410 means the endpoint this library calls no longer exists, so it signals
    that pyicloud needs updating rather than that the caller's request,
    credentials, or session were wrong.

    Distinguishing it matters because the alternative is expensive: when the
    Photos upload endpoint was withdrawn it surfaced as a generic response
    error, and the reporter ruled out a stale session, an outdated client, a
    missing PCS handshake, the wrong dsid, and the wrong partition before
    concluding the endpoint itself had gone.

    ``endpoint`` is a redacted host-and-path label safe to quote in a bug
    report; the full URL and the response body are available on ``response``
    when one was captured.
    """

    def __init__(self, endpoint: str, response: Response | None = None) -> None:
        """Describe a withdrawn endpoint and point at the issue tracker."""
        self.endpoint: str = endpoint
        super().__init__(
            f"Apple no longer serves {endpoint}. This endpoint appears to have "
            "been withdrawn, which means pyicloud needs updating rather than "
            "that your request was wrong. Please report it at "
            "https://github.com/timlaing/pyicloud/issues and quote this endpoint",
            HTTP_GONE,
        )
        # The response is deliberately withheld from the parent, which would
        # append ``response.text`` to the message. This message exists to be
        # pasted into a public issue, and Apple's error bodies can carry the
        # dsid and session identifiers that ``endpoint`` was redacted to keep
        # out of it. Attaching it here keeps the body reachable for debugging
        # without folding it into quotable text.
        self.response = response

PyiCloudFailedLoginException

Bases: PyiCloudException

iCloud failed login exception.

Source code in pyicloud/exceptions.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class PyiCloudFailedLoginException(PyiCloudException):
    """iCloud failed login exception."""

    def __init__(
        self,
        msg: str,
        *args: Any,
        response: Response | None = None,
    ) -> None:
        """Initialize a login failure with optional HTTP response details."""
        self.response: Response | None = response
        message: str = msg or "Failed login to iCloud"
        if response is not None and response.text:
            message = f"{message} ({response.status_code}): {response.text}"
        super().__init__(message, *args)

PyiCloudAccountLockedException

Bases: PyiCloudFailedLoginException

Apple rejected authentication because the account is locked.

The iCloud sign-in flow directs locked accounts to this unlock URL. Apple determines the required verification steps. Replace {url_encoded_apple_account} with the URL-encoded Apple Account: https://iforgot.apple.com/password/verify/appleid?returnUrl=https%3A%2F%2Fwww.icloud.com%2F&prs_account_nm={url_encoded_apple_account}&unlock=true.

Source code in pyicloud/exceptions.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class PyiCloudAccountLockedException(PyiCloudFailedLoginException):
    """Apple rejected authentication because the account is locked.

    The iCloud sign-in flow directs locked accounts to this unlock URL. Apple
    determines the required verification steps. Replace
    ``{url_encoded_apple_account}`` with the URL-encoded Apple Account:
    ``https://iforgot.apple.com/password/verify/appleid?returnUrl=https%3A%2F%2Fwww.icloud.com%2F&prs_account_nm={url_encoded_apple_account}&unlock=true``.
    """

    def __init__(self, code: int | str, response: Response) -> None:
        """Keep the service error and response available without logging its body."""
        self.code: int | str = code
        super().__init__("Apple Account is locked for security reasons")
        self.response = response

PyiCloudAcceptTermsException

Bases: PyiCloudException

iCloud accept terms exception.

Source code in pyicloud/exceptions.py
157
158
class PyiCloudAcceptTermsException(PyiCloudException):
    """iCloud accept terms exception."""

PyiCloud2FARequiredException

Bases: PyiCloudException

iCloud 2FA required exception.

Source code in pyicloud/exceptions.py
161
162
163
164
165
166
167
168
class PyiCloud2FARequiredException(PyiCloudException):
    """iCloud 2FA required exception."""

    def __init__(self, apple_id: str, response: Response) -> None:
        """Initialize a 2FA-required error for an HSA2 login challenge."""
        message: str = f"2FA authentication required for account: {apple_id} (HSA2)"
        super().__init__(message)
        self.response: Response = response

PyiCloud2SARequiredException

Bases: PyiCloudException

iCloud 2SA required exception.

Source code in pyicloud/exceptions.py
171
172
173
174
175
176
177
class PyiCloud2SARequiredException(PyiCloudException):
    """iCloud 2SA required exception."""

    def __init__(self, apple_id: str) -> None:
        """Initialize a 2SA-required error for a legacy login challenge."""
        message: str = f"Two-step authentication required for account: {apple_id}"
        super().__init__(message)

PyiCloudAuthRequiredException

Bases: PyiCloudException

iCloud re-authentication required exception.

Source code in pyicloud/exceptions.py
180
181
182
183
184
185
186
187
class PyiCloudAuthRequiredException(PyiCloudException):
    """iCloud re-authentication required exception."""

    def __init__(self, apple_id: str, response: Response) -> None:
        """Initialize a reauthentication-required error with the triggering response."""
        message: str = f"Re-authentication required for account: {apple_id}"
        super().__init__(message)
        self.response: Response = response

PyiCloudNoTrustedNumberAvailable

Bases: PyiCloudException

iCloud no trusted number exception.

Source code in pyicloud/exceptions.py
190
191
class PyiCloudNoTrustedNumberAvailable(PyiCloudException):
    """iCloud no trusted number exception."""

PyiCloudTrustedDevicePromptException

Bases: PyiCloudAPIResponseException

Trusted-device prompt bootstrap exception.

Source code in pyicloud/exceptions.py
194
195
class PyiCloudTrustedDevicePromptException(PyiCloudAPIResponseException):
    """Trusted-device prompt bootstrap exception."""

PyiCloudTrustedDeviceVerificationException

Bases: PyiCloudAPIResponseException

Trusted-device bridge verification exception.

Source code in pyicloud/exceptions.py
198
199
class PyiCloudTrustedDeviceVerificationException(PyiCloudAPIResponseException):
    """Trusted-device bridge verification exception."""

PyiCloudTrustedDeviceChallengeRequiredException

Bases: PyiCloudTrustedDeviceVerificationException

The active 2FA challenge is unusable and a replacement must be requested.

Raised when a trusted-device bridge challenge has been consumed or cannot be verified. Retrying without a fresh challenge would silently fall back to the legacy verifier, so callers should request and deliver a new code first.

Source code in pyicloud/exceptions.py
202
203
204
205
206
207
208
209
210
class PyiCloudTrustedDeviceChallengeRequiredException(
    PyiCloudTrustedDeviceVerificationException
):
    """The active 2FA challenge is unusable and a replacement must be requested.

    Raised when a trusted-device bridge challenge has been consumed or cannot be
    verified. Retrying without a fresh challenge would silently fall back to the
    legacy verifier, so callers should request and deliver a new code first.
    """

PyiCloudSecurityKeyException

Bases: PyiCloudException

Security key confirmation exception.

Source code in pyicloud/exceptions.py
213
214
class PyiCloudSecurityKeyException(PyiCloudException):
    """Security key confirmation exception."""

PyiCloudSecurityKeyTimeoutException

Bases: PyiCloudSecurityKeyException

Security key confirmation cancelled or timed out before the key was touched.

Source code in pyicloud/exceptions.py
217
218
class PyiCloudSecurityKeyTimeoutException(PyiCloudSecurityKeyException):
    """Security key confirmation cancelled or timed out before the key was touched."""

PyiCloudNoStoredPasswordAvailableException

Bases: PyiCloudException

iCloud no stored password exception.

Source code in pyicloud/exceptions.py
221
222
class PyiCloudNoStoredPasswordAvailableException(PyiCloudException):
    """iCloud no stored password exception."""

PyiCloudNoDevicesException

Bases: PyiCloudException

iCloud no device exception.

Source code in pyicloud/exceptions.py
226
227
class PyiCloudNoDevicesException(PyiCloudException):
    """iCloud no device exception."""