Errors are Exceptions

[breaking-change]
- PyiCloudAPIResponseError --> PyiCloudAPIResponseException
- PyiCloud2SARequiredError --> PyiCloud2SARequiredException
- NoStoredPasswordAvailable --> PyiCloudNoStoredPasswordAvailableException
- PyiCloudServiceNotActivatedErrror --> PyiCloudServiceNotActivatedException
This commit is contained in:
Quentin POLLET 2020-03-09 22:22:04 +01:00 committed by Quentame
parent 0e26d4e947
commit 412cfd8c7e
4 changed files with 21 additions and 21 deletions

View file

@ -12,9 +12,9 @@ from re import match
from pyicloud.exceptions import (
PyiCloudFailedLoginException,
PyiCloudAPIResponseError,
PyiCloud2SARequiredError,
PyiCloudServiceNotActivatedErrror
PyiCloudAPIResponseException,
PyiCloud2SARequiredException,
PyiCloudServiceNotActivatedException
)
from pyicloud.services import (
FindMyiPhoneServiceManager,
@ -73,7 +73,7 @@ class PyiCloudSession(requests.Session):
if not response.ok and content_type not in json_mimetypes:
if kwargs.get('retried') is None and response.status_code == 450:
api_error = PyiCloudAPIResponseError(
api_error = PyiCloudAPIResponseException(
response.reason,
response.status_code,
retry=True
@ -114,11 +114,11 @@ class PyiCloudSession(requests.Session):
def _raise_error(self, code, reason):
if self.service.requires_2sa and \
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':
reason = 'Please log into https://icloud.com/ to manually ' \
'finish setting up your iCloud service'
api_error = PyiCloudServiceNotActivatedErrror(reason, code)
api_error = PyiCloudServiceNotActivatedException(reason, code)
logger.error(api_error)
raise(api_error)
@ -127,7 +127,7 @@ class PyiCloudSession(requests.Session):
'again. The remote servers might be trying to ' \
'throttle requests.'
api_error = PyiCloudAPIResponseError(reason, code)
api_error = PyiCloudAPIResponseException(reason, code)
logger.error(api_error)
raise api_error
@ -221,7 +221,7 @@ class PyiCloudService(object):
params=self.params,
data=json.dumps(data)
)
except PyiCloudAPIResponseError as error:
except PyiCloudAPIResponseException as error:
msg = 'Invalid email/password combination.'
raise PyiCloudFailedLoginException(msg, error)
@ -286,7 +286,7 @@ class PyiCloudService(object):
params=self.params,
data=data
)
except PyiCloudAPIResponseError as error:
except PyiCloudAPIResponseException as error:
if error.code == -21669:
# Wrong verification code
return False

View file

@ -7,7 +7,7 @@ class PyiCloudNoDevicesException(PyiCloudException):
pass
class PyiCloudAPIResponseError(PyiCloudException):
class PyiCloudAPIResponseException(PyiCloudException):
def __init__(self, reason, code, retry=False):
self.reason = reason
self.code = code
@ -17,22 +17,22 @@ class PyiCloudAPIResponseError(PyiCloudException):
if retry:
message += ". Retrying ..."
super(PyiCloudAPIResponseError, self).__init__(message)
super(PyiCloudAPIResponseException, self).__init__(message)
class PyiCloudFailedLoginException(PyiCloudException):
pass
class PyiCloud2SARequiredError(PyiCloudException):
class PyiCloud2SARequiredException(PyiCloudException):
def __init__(self, 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
class PyiCloudServiceNotActivatedErrror(PyiCloudAPIResponseError):
class PyiCloudServiceNotActivatedException(PyiCloudAPIResponseException):
pass

View file

@ -4,7 +4,7 @@ import logging
import base64
from datetime import datetime
from pyicloud.exceptions import PyiCloudServiceNotActivatedErrror
from pyicloud.exceptions import PyiCloudServiceNotActivatedException
import pytz
from future.moves.urllib.parse import urlencode
@ -159,7 +159,7 @@ class PhotosService(object):
response = request.json()
indexing_state = response['records'][0]['fields']['state']['value']
if indexing_state != 'FINISHED':
raise PyiCloudServiceNotActivatedErrror(
raise PyiCloudServiceNotActivatedException(
('iCloud Photo Library not finished indexing. Please try '
'again in a few minutes'), None)

View file

@ -2,7 +2,7 @@ import getpass
import keyring
import sys
from .exceptions import NoStoredPasswordAvailable
from .exceptions import PyiCloudNoStoredPasswordAvailableException
KEYRING_SYSTEM = 'pyicloud://icloud-password'
@ -11,7 +11,7 @@ KEYRING_SYSTEM = 'pyicloud://icloud-password'
def get_password(username, interactive=sys.stdout.isatty()):
try:
return get_password_from_keyring(username)
except NoStoredPasswordAvailable:
except PyiCloudNoStoredPasswordAvailableException:
if not interactive:
raise
@ -25,7 +25,7 @@ def get_password(username, interactive=sys.stdout.isatty()):
def password_exists_in_keyring(username):
try:
get_password_from_keyring(username)
except NoStoredPasswordAvailable:
except PyiCloudNoStoredPasswordAvailableException:
return False
return True
@ -37,7 +37,7 @@ def get_password_from_keyring(username):
username
)
if result is None:
raise NoStoredPasswordAvailable(
raise PyiCloudNoStoredPasswordAvailableException(
"No pyicloud password for {username} could be found "
"in the system keychain. Use the `--store-in-keyring` "
"command-line option for storing a password for this "