Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _exception_for_5xx_status(self, status, uri):
return HTTPError(
'Received a server error (%(status)i) for '
'%(uri)s' % locals(), status, uri)
elif response.headers['Content-Type'].find('json') == -1:
return HTTPError(
'Received a %i for %s with the following '
'body: %s' % (status, uri, response.content), status, uri)
try:
body = response.json()
except ValueError as ex:
return HTTPError(
'Received a %(status)i error for %(uri)s but it did'
' not include the expected JSON body: ' % locals() +
', '.join(ex.args), status, uri)
else:
if 'code' in body and 'error' in body:
return self._exception_for_web_service_error(
body.get('error'), body.get('code'), status, uri)
return HTTPError(
'Response contains JSON but it does not specify '
'code or error keys', status, uri)
def __init__(self, message, http_status=None, uri=None):
super(HTTPError, self).__init__(message)
self.http_status = http_status
self.uri = uri
def _exception_for_non_200_status(self, status, uri):
return HTTPError(
'Received a very surprising HTTP status '
'(%(status)i) for %(uri)s' % locals(), status, uri)
def _exception_for_4xx_status(self, response, status, uri):
if not response.content:
return HTTPError(
'Received a %(status)i error for %(uri)s '
'with no body.' % locals(), status, uri)
elif response.headers['Content-Type'].find('json') == -1:
return HTTPError(
'Received a %i for %s with the following '
'body: %s' % (status, uri, response.content), status, uri)
try:
body = response.json()
except ValueError as ex:
return HTTPError(
'Received a %(status)i error for %(uri)s but it did'
' not include the expected JSON body: ' % locals() +
', '.join(ex.args), status, uri)
else:
if 'code' in body and 'error' in body:
return self._exception_for_web_service_error(
body.get('error'), body.get('code'), status, uri)
return HTTPError(
'Response contains JSON but it does not specify '
'code or error keys', status, uri)
def _exception_for_4xx_status(self, response, status, uri):
if not response.content:
return HTTPError(
'Received a %(status)i error for %(uri)s '
'with no body.' % locals(), status, uri)
elif response.headers['Content-Type'].find('json') == -1:
return HTTPError(
'Received a %i for %s with the following '
'body: %s' % (status, uri, response.content), status, uri)
try:
body = response.json()
except ValueError as ex:
return HTTPError(
'Received a %(status)i error for %(uri)s but it did'
' not include the expected JSON body: ' % locals() +
', '.join(ex.args), status, uri)
else:
if 'code' in body and 'error' in body:
return self._exception_for_web_service_error(