Skip to content

Commit 1bbbbee

Browse files
author
Elias Nygren
committed
UpcloudAPIError
1 parent 953dfa9 commit 1bbbbee

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

upcloud_api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
__license__ = 'MIT'
1414
__copyright__ = 'Copyright (c) 2015 Elias Nygren'
1515

16+
from upcloud_api.errors import UpCloudAPIError
1617
from upcloud_api.storage import Storage
1718
from upcloud_api.ip_address import IP_address
1819
from upcloud_api.server import Server, login_user_block

upcloud_api/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from __future__ import unicode_literals
2+
23
import json
34
import requests
45

6+
from upcloud_api import UpCloudAPIError
7+
58

69
class BaseAPI(object):
710
"""
@@ -65,6 +68,8 @@ def __error_middleware(self, res, res_json):
6568
Middleware that raises an exception when HTTP statuscode is an error code.
6669
"""
6770
if(res.status_code in [400, 401, 402, 403, 404, 405, 406, 409]):
68-
raise Exception(res_json)
71+
err_dict = res_json.get('error', {})
72+
raise UpCloudAPIError(error_code=err_dict.get('error_code'),
73+
error_message=err_dict.get('error_message'))
6974

7075
return res_json

upcloud_api/errors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
class UpCloudAPIError(Exception):
3+
"""Custom Error class for UpCloud API error responses.
4+
5+
Each API call returns an `error_code` and `error_message` that
6+
are available as attributes via instances of this class.
7+
"""
8+
def __init__(self, error_code, error_message):
9+
self.error_code = error_code
10+
self.error_message = error_message
11+
12+
def __str__(self):
13+
return '{0} {1}'.format(self.error_code, self.error_message)

0 commit comments

Comments
 (0)