Skip to content

Commit d3625e3

Browse files
committed
Closes #24
1 parent b336819 commit d3625e3

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

hapiclient/util.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ def urlopen(url):
273273
"""Wrapper to request.get() in urllib3"""
274274

275275
import sys
276+
import urllib3
276277
from json import load
277278

278279
# https://stackoverflow.com/a/2020083
@@ -282,27 +283,35 @@ def get_full_class_name(obj):
282283
return obj.__class__.__name__
283284
return module + '.' + obj.__class__.__name__
284285

285-
import urllib3
286286
c = " If problem persists, a contact email for the server may be listed "
287287
c = c + "at http://hapi-server.org/servers/"
288+
msg = '';
288289
try:
289290
http = urllib3.PoolManager()
290291
res = http.request('GET', url, preload_content=False, retries=2)
291292
if res.status != 200:
292293
try:
293294
jres = load(res)
294-
if 'status' in jres:
295-
if 'message' in jres['status']:
296-
error('\n%s\n %s\n' % (url, jres['status']['message']))
297-
error("Problem with " + url + \
298-
". Server responded with non-200 HTTP status (" \
295+
except Exception as e:
296+
msg = "Problem with " + url + \
297+
". Server responded with non-200 HTTP status (" \
299298
+ str(res.status) + \
300-
") and invalid HAPI JSON error message in response body." + c)
301-
except:
302-
error("Problem with " + url + \
299+
") and an invalid JSON in response body." + c
300+
301+
if msg == '' and 'status' in jres:
302+
if 'message' in jres['status']:
303+
msg = '%s\n' % (jres['status']['message'])
304+
305+
if msg == '':
306+
msg = "Problem with " + url + \
303307
". Server responded with non-200 HTTP status (" + \
304308
str(res.status) + \
305-
") and no HAPI JSON error message in response body." + c)
309+
") but no JSON without HAPI error message in response body." + c
310+
311+
raise HAPIError
312+
313+
except HAPIError:
314+
error(msg)
306315
except urllib3.exceptions.NewConnectionError:
307316
error('Connection error for : ' + url + c)
308317
except urllib3.exceptions.ConnectTimeoutError:
@@ -318,7 +327,7 @@ def get_full_class_name(obj):
318327
except urllib3.exceptions.HTTPError as e:
319328
error('Exception ' + get_full_class_name(e) + " for: " + url)
320329
except Exception as e:
321-
error(type(sys.exc_info()[1]).__name__ + ': ' \
330+
print(type(sys.exc_info()[1]).__name__ + ': ' \
322331
+ str(e) + ' for URL: ' + url)
323332

324333
return res

0 commit comments

Comments
 (0)