Skip to content

Commit

Permalink
Store errors in errs dict
Browse files Browse the repository at this point in the history
  • Loading branch information
nibrag committed Jun 9, 2016
1 parent adf63ff commit c5bc20a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
45 changes: 25 additions & 20 deletions aio_anticaptcha/__init__.py
Expand Up @@ -163,27 +163,32 @@ def _create_session(self):
return aiohttp.ClientSession(loop=self._loop)

def _handle_error(self, msg):
msg = msg.upper()
if msg.startswith('ERROR_'):
if msg == 'ERROR_WRONG_USER_KEY':
raise UserKeyError('Account authorization key is invalid')
if msg == 'ERROR_KEY_DOES_NOT_EXIST':
raise UserKeyError('Account authorization key '
'not found in the system')
if msg == 'ERROR_ZERO_BALANCE':
raise ZeroBalanceError('Account has zero or negative balance')
if msg == 'ERROR_ZERO_CAPTCHA_FILESIZE':
raise ServiceError('The size of the captcha you are '
'uploading is less than 100 bytes.')
if msg == 'ERROR_IMAGE_TYPE_NOT_SUPPORTED':
raise ServiceError('Could not determine captcha file type')
if msg == 'ERROR_IP_NOT_ALLOWED':
raise ServiceError('Request with current account key '
'is not allowed from your IP')
if msg == 'ERROR_NO_SUCH_CAPCHA_ID':
raise ServiceError('Captcha with such ID was '
'not found in the system')
if msg == 'ERROR_NO_REQUEST_ACTION_RECEIVED':
raise ServiceError('Not request action received')
errs = {
'ERROR_WRONG_USER_KEY':
UserKeyError('Account authorization key is invalid'),
'ERROR_KEY_DOES_NOT_EXIST':
UserKeyError('Account authorization key '
'not found in the system'),
'ERROR_ZERO_BALANCE':
ZeroBalanceError('Account has zero or negative balance'),
'ERROR_ZERO_CAPTCHA_FILESIZE':
ServiceError('The size of the captcha you are '
'uploading is less than 100 bytes.'),
'ERROR_IMAGE_TYPE_NOT_SUPPORTED':
ServiceError('Could not determine captcha file type'),
'ERROR_IP_NOT_ALLOWED':
ServiceError('Request with current account key '
'is not allowed from your IP'),
'ERROR_NO_SUCH_CAPCHA_ID':
ServiceError('Captcha with such ID was '
'not found in the system'),
'ERROR_NO_REQUEST_ACTION_RECEIVED':
ServiceError('No request action received')
}
if msg in errs:
raise errs[msg]

def __enter__(self):
return self
Expand Down
10 changes: 5 additions & 5 deletions tests/test_functional.py
Expand Up @@ -108,7 +108,7 @@ def test_handle_error(self):

with self.assertRaises(ServiceError) as cm:
ag._handle_error('ERROR_NO_REQUEST_ACTION_RECEIVED')
self.assertIn('Not request action received', str(cm.exception))
self.assertIn('No request action received', str(cm.exception))
ag.close()

def test_abuse_http_err(self):
Expand All @@ -128,7 +128,7 @@ def test_abuse_handle_error(self):

with self.assertRaises(ServiceError) as cm:
self.loop.run_until_complete(ag.abuse(123))
self.assertIn('Not request action received', str(cm.exception))
self.assertIn('No request action received', str(cm.exception))

def test_abuse_client_error(self):
ag = AntiCaptcha(api_key, loop=self.loop)
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_get_balance_handle_error(self):

with self.assertRaises(ServiceError) as cm:
self.loop.run_until_complete(ag.get_balance())
self.assertIn('Not request action received', str(cm.exception))
self.assertIn('No request action received', str(cm.exception))

def test_get_balance_client_error(self):
ag = AntiCaptcha(api_key, loop=self.loop)
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_get_captcha_handle_error(self):

with self.assertRaises(ServiceError) as cm:
self.loop.run_until_complete(ag._get_captcha('id'))
self.assertIn('Not request action received', str(cm.exception))
self.assertIn('No request action received', str(cm.exception))

def test_get_captcha_client_error(self):
ag = AntiCaptcha(api_key, loop=self.loop)
Expand Down Expand Up @@ -314,7 +314,7 @@ def test_send_captcha_handle_err(self):

with self.assertRaises(ServiceError) as cm:
self.loop.run_until_complete(ag._send_captcha(b'id'))
self.assertIn('Not request action received', str(cm.exception))
self.assertIn('No request action received', str(cm.exception))

def test_send_captcha_client_error(self):
ag = AntiCaptcha(api_key, loop=self.loop)
Expand Down

0 comments on commit c5bc20a

Please sign in to comment.