Errors are Exceptions
[breaking-change] - PyiCloudAPIResponseError --> PyiCloudAPIResponseException - PyiCloud2SARequiredError --> PyiCloud2SARequiredException - NoStoredPasswordAvailable --> PyiCloudNoStoredPasswordAvailableException - PyiCloudServiceNotActivatedErrror --> PyiCloudServiceNotActivatedException
This commit is contained in:
parent
0e26d4e947
commit
412cfd8c7e
4 changed files with 21 additions and 21 deletions
|
@ -12,9 +12,9 @@ from re import match
|
||||||
|
|
||||||
from pyicloud.exceptions import (
|
from pyicloud.exceptions import (
|
||||||
PyiCloudFailedLoginException,
|
PyiCloudFailedLoginException,
|
||||||
PyiCloudAPIResponseError,
|
PyiCloudAPIResponseException,
|
||||||
PyiCloud2SARequiredError,
|
PyiCloud2SARequiredException,
|
||||||
PyiCloudServiceNotActivatedErrror
|
PyiCloudServiceNotActivatedException
|
||||||
)
|
)
|
||||||
from pyicloud.services import (
|
from pyicloud.services import (
|
||||||
FindMyiPhoneServiceManager,
|
FindMyiPhoneServiceManager,
|
||||||
|
@ -73,7 +73,7 @@ class PyiCloudSession(requests.Session):
|
||||||
|
|
||||||
if not response.ok and content_type not in json_mimetypes:
|
if not response.ok and content_type not in json_mimetypes:
|
||||||
if kwargs.get('retried') is None and response.status_code == 450:
|
if kwargs.get('retried') is None and response.status_code == 450:
|
||||||
api_error = PyiCloudAPIResponseError(
|
api_error = PyiCloudAPIResponseException(
|
||||||
response.reason,
|
response.reason,
|
||||||
response.status_code,
|
response.status_code,
|
||||||
retry=True
|
retry=True
|
||||||
|
@ -114,11 +114,11 @@ class PyiCloudSession(requests.Session):
|
||||||
def _raise_error(self, code, reason):
|
def _raise_error(self, code, reason):
|
||||||
if self.service.requires_2sa and \
|
if self.service.requires_2sa and \
|
||||||
reason == 'Missing X-APPLE-WEBAUTH-TOKEN cookie':
|
reason == 'Missing X-APPLE-WEBAUTH-TOKEN cookie':
|
||||||
raise PyiCloud2SARequiredError(self.service.user['apple_id'])
|
raise PyiCloud2SARequiredException(self.service.user['apple_id'])
|
||||||
if code == 'ZONE_NOT_FOUND' or code == 'AUTHENTICATION_FAILED':
|
if code == 'ZONE_NOT_FOUND' or code == 'AUTHENTICATION_FAILED':
|
||||||
reason = 'Please log into https://icloud.com/ to manually ' \
|
reason = 'Please log into https://icloud.com/ to manually ' \
|
||||||
'finish setting up your iCloud service'
|
'finish setting up your iCloud service'
|
||||||
api_error = PyiCloudServiceNotActivatedErrror(reason, code)
|
api_error = PyiCloudServiceNotActivatedException(reason, code)
|
||||||
logger.error(api_error)
|
logger.error(api_error)
|
||||||
|
|
||||||
raise(api_error)
|
raise(api_error)
|
||||||
|
@ -127,7 +127,7 @@ class PyiCloudSession(requests.Session):
|
||||||
'again. The remote servers might be trying to ' \
|
'again. The remote servers might be trying to ' \
|
||||||
'throttle requests.'
|
'throttle requests.'
|
||||||
|
|
||||||
api_error = PyiCloudAPIResponseError(reason, code)
|
api_error = PyiCloudAPIResponseException(reason, code)
|
||||||
logger.error(api_error)
|
logger.error(api_error)
|
||||||
raise api_error
|
raise api_error
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ class PyiCloudService(object):
|
||||||
params=self.params,
|
params=self.params,
|
||||||
data=json.dumps(data)
|
data=json.dumps(data)
|
||||||
)
|
)
|
||||||
except PyiCloudAPIResponseError as error:
|
except PyiCloudAPIResponseException as error:
|
||||||
msg = 'Invalid email/password combination.'
|
msg = 'Invalid email/password combination.'
|
||||||
raise PyiCloudFailedLoginException(msg, error)
|
raise PyiCloudFailedLoginException(msg, error)
|
||||||
|
|
||||||
|
@ -286,7 +286,7 @@ class PyiCloudService(object):
|
||||||
params=self.params,
|
params=self.params,
|
||||||
data=data
|
data=data
|
||||||
)
|
)
|
||||||
except PyiCloudAPIResponseError as error:
|
except PyiCloudAPIResponseException as error:
|
||||||
if error.code == -21669:
|
if error.code == -21669:
|
||||||
# Wrong verification code
|
# Wrong verification code
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -7,7 +7,7 @@ class PyiCloudNoDevicesException(PyiCloudException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PyiCloudAPIResponseError(PyiCloudException):
|
class PyiCloudAPIResponseException(PyiCloudException):
|
||||||
def __init__(self, reason, code, retry=False):
|
def __init__(self, reason, code, retry=False):
|
||||||
self.reason = reason
|
self.reason = reason
|
||||||
self.code = code
|
self.code = code
|
||||||
|
@ -17,22 +17,22 @@ class PyiCloudAPIResponseError(PyiCloudException):
|
||||||
if retry:
|
if retry:
|
||||||
message += ". Retrying ..."
|
message += ". Retrying ..."
|
||||||
|
|
||||||
super(PyiCloudAPIResponseError, self).__init__(message)
|
super(PyiCloudAPIResponseException, self).__init__(message)
|
||||||
|
|
||||||
|
|
||||||
class PyiCloudFailedLoginException(PyiCloudException):
|
class PyiCloudFailedLoginException(PyiCloudException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PyiCloud2SARequiredError(PyiCloudException):
|
class PyiCloud2SARequiredException(PyiCloudException):
|
||||||
def __init__(self, apple_id):
|
def __init__(self, apple_id):
|
||||||
message = "Two-step authentication required for account: %s" % apple_id
|
message = "Two-step authentication required for account: %s" % apple_id
|
||||||
super(PyiCloud2SARequiredError, self).__init__(message)
|
super(PyiCloud2SARequiredException, self).__init__(message)
|
||||||
|
|
||||||
|
|
||||||
class NoStoredPasswordAvailable(PyiCloudException):
|
class PyiCloudNoStoredPasswordAvailableException(PyiCloudException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PyiCloudServiceNotActivatedErrror(PyiCloudAPIResponseError):
|
class PyiCloudServiceNotActivatedException(PyiCloudAPIResponseException):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -4,7 +4,7 @@ import logging
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pyicloud.exceptions import PyiCloudServiceNotActivatedErrror
|
from pyicloud.exceptions import PyiCloudServiceNotActivatedException
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from future.moves.urllib.parse import urlencode
|
from future.moves.urllib.parse import urlencode
|
||||||
|
@ -159,7 +159,7 @@ class PhotosService(object):
|
||||||
response = request.json()
|
response = request.json()
|
||||||
indexing_state = response['records'][0]['fields']['state']['value']
|
indexing_state = response['records'][0]['fields']['state']['value']
|
||||||
if indexing_state != 'FINISHED':
|
if indexing_state != 'FINISHED':
|
||||||
raise PyiCloudServiceNotActivatedErrror(
|
raise PyiCloudServiceNotActivatedException(
|
||||||
('iCloud Photo Library not finished indexing. Please try '
|
('iCloud Photo Library not finished indexing. Please try '
|
||||||
'again in a few minutes'), None)
|
'again in a few minutes'), None)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import getpass
|
||||||
import keyring
|
import keyring
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .exceptions import NoStoredPasswordAvailable
|
from .exceptions import PyiCloudNoStoredPasswordAvailableException
|
||||||
|
|
||||||
|
|
||||||
KEYRING_SYSTEM = 'pyicloud://icloud-password'
|
KEYRING_SYSTEM = 'pyicloud://icloud-password'
|
||||||
|
@ -11,7 +11,7 @@ KEYRING_SYSTEM = 'pyicloud://icloud-password'
|
||||||
def get_password(username, interactive=sys.stdout.isatty()):
|
def get_password(username, interactive=sys.stdout.isatty()):
|
||||||
try:
|
try:
|
||||||
return get_password_from_keyring(username)
|
return get_password_from_keyring(username)
|
||||||
except NoStoredPasswordAvailable:
|
except PyiCloudNoStoredPasswordAvailableException:
|
||||||
if not interactive:
|
if not interactive:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ def get_password(username, interactive=sys.stdout.isatty()):
|
||||||
def password_exists_in_keyring(username):
|
def password_exists_in_keyring(username):
|
||||||
try:
|
try:
|
||||||
get_password_from_keyring(username)
|
get_password_from_keyring(username)
|
||||||
except NoStoredPasswordAvailable:
|
except PyiCloudNoStoredPasswordAvailableException:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -37,7 +37,7 @@ def get_password_from_keyring(username):
|
||||||
username
|
username
|
||||||
)
|
)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise NoStoredPasswordAvailable(
|
raise PyiCloudNoStoredPasswordAvailableException(
|
||||||
"No pyicloud password for {username} could be found "
|
"No pyicloud password for {username} could be found "
|
||||||
"in the system keychain. Use the `--store-in-keyring` "
|
"in the system keychain. Use the `--store-in-keyring` "
|
||||||
"command-line option for storing a password for this "
|
"command-line option for storing a password for this "
|
||||||
|
|
Loading…
Reference in a new issue