Skip to content

Commit 32ea169

Browse files
committed
Refactored ErrorHandler Methods
Looks neater now! Also fixed some bugs in output.
1 parent ab579c4 commit 32ea169

File tree

2 files changed

+69
-71
lines changed

2 files changed

+69
-71
lines changed

blockscore/http_client/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def request(self, path, body, method, options):
8787
if method != 'get':
8888
kwargs = self.set_body(kwargs)
8989

90-
kwargs['hooks'] = dict(response=ErrorHandler.check_error)
90+
kwargs['hooks'] = dict(response=ErrorHandler().check_error)
9191

9292
kwargs = self.auth.set(kwargs)
9393

@@ -130,4 +130,4 @@ def dict_key_lower(self, dic):
130130

131131
# Make a function for lowercase
132132
def key_lower(self, key):
133-
return key.lower()
133+
return key.lower()
Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,71 @@
11
from ..error import BlockscoreError, AuthorizationError, \
2-
InternalServerError, ValidationError, ParameterError, NotFoundError
2+
InternalServerError, ValidationError, ParameterError, NotFoundError
33
from .response_handler import ResponseHandler
4-
4+
55
# ErrorHanlder takes care of selecting the error message from response body
66
class ErrorHandler():
7-
8-
@staticmethod
9-
def check_error(response, *args, **kwargs):
10-
code = response.status_code
11-
typ = response.headers.get('content-type')
12-
13-
# No error found
14-
if (200 <= code < 300):
15-
return
16-
17-
body = ResponseHandler.get_body(response)
18-
message = ''
19-
error_type = None
20-
error_code = None
21-
param = None
22-
23-
# If HTML, whole body is taken
24-
if isinstance(body, str):
25-
message = body
26-
27-
# If JSON, a particular field is taken and used
28-
if typ.find('json') != -1 and isinstance(body, dict):
29-
30-
if 'error' in body:
31-
error = body['error']
32-
33-
message = error['message']
34-
error_type = error['type']
35-
36-
if 'code' in error.keys():
37-
error_code = error['code']
38-
if 'param' in error.keys():
39-
param = error['param']
40-
41-
else:
42-
message = 'Unable to select error message from json returned by request responsible for error'
43-
44-
if message == '':
45-
message = 'Unable to understand the content type of response returned by request responsible for error'
46-
47-
48-
if code == 400:
49-
# Inputs could not be validated
50-
if param is not None:
51-
raise ValidationError(message, body, param, error_type, error_code)
52-
53-
# Required parameter missing
54-
else:
55-
raise ParameterError(message, body, error_type)
56-
57-
# Trying to access nonexistent endpoint
58-
elif code == 404:
59-
raise NotFoundError(message, body, error_type)
60-
61-
# Error with an API Key
62-
elif code == 401:
63-
raise AuthorizationError(message, body, error_type)
64-
65-
# Internal API Error
66-
elif code == 500:
67-
raise InternalServerError(message, body, error_type)
68-
69-
# Generic BlockscoreError (fallback)
70-
else:
71-
raise BlockscoreError(message, body)
72-
73-
7+
def check_error(self, response, *args, **kwargs):
8+
code = response.status_code
9+
typ = response.headers.get('content-type')
10+
11+
# No error found
12+
if (200 <= code < 300):
13+
return
14+
self.body = ResponseHandler.get_body(response)
15+
self.message = self.get_message(self.body)
16+
self.error_type = self.error_code = self.param = None
17+
18+
# determines if an error is in the response's body
19+
if 'error' in self.body.keys():
20+
error = self.body['error']
21+
self.error_type = self.get_value(error, 'type')
22+
self.error_code = self.get_value(error, 'code')
23+
self.param = self.get_value(error, 'param')
24+
25+
# raises the appropriate error if necessary
26+
self.process_code(code)
27+
28+
def process_code(self, code):
29+
30+
if code == 400:
31+
# Inputs could not be validated
32+
if self.param is not None:
33+
raise ValidationError(self.message, self.body, self.param, \
34+
self.error_type, self.error_code)
35+
# Required parameter missing
36+
else:
37+
raise ParameterError(self.message, self.body, self.error_type)
38+
# Trying to access nonexistent endpoint
39+
elif code == 404:
40+
raise NotFoundError(self.message, self.body, self.error_type)
41+
# Error with an API Key
42+
elif code == 401:
43+
raise AuthorizationError(self.message, self.body, self.error_type)
44+
# Internal API Error
45+
elif code == 500:
46+
raise InternalServerError(self.message, self.body, self.error_type)
47+
# Generic BlockscoreError (fallback)
48+
else:
49+
raise BlockscoreError(self.message, self.body)
50+
51+
@staticmethod
52+
def get_message(body):
53+
message = ''
54+
# If HTML, whole body is taken
55+
if isinstance(body, str):
56+
message = body
57+
elif isinstance(body, dict): # body not str
58+
if 'error' in body.keys():
59+
message = body['error']['message']
60+
else:
61+
message = 'Unable to select error message from json returned by request responsible for error'
62+
63+
else: # body not str or dict
64+
message = 'Unable to understand the content type of response returned by request responsible for error'
65+
return message
66+
67+
@staticmethod
68+
def get_value(obj, key):
69+
if key in obj.keys():
70+
return obj[key]
71+
return None

0 commit comments

Comments
 (0)