Handle request failures more gracefully

This commit is contained in:
Joshua Boniface 2020-02-19 16:19:34 -05:00
parent 1b8b32b07c
commit 65932b20d2
1 changed files with 10 additions and 1 deletions

View File

@ -23,6 +23,14 @@
import requests
import click
class ErrorResponse(requests.Response):
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
def call_api(config, operation, request_uri, params=None, data=None, files=None):
# Craft the URI
uri = '{}://{}{}{}'.format(
@ -78,7 +86,8 @@ def call_api(config, operation, request_uri, params=None, data=None, files=None)
data=data
)
except Exception as e:
return 'Failed to connect to the API: {}'.format(e)
message = 'Failed to connect to the API: {}'.format(e)
response = ErrorResponse({'message':message}, 500)
# Display debug output
if config['debug']: