Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include response headers in exceptions #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions chargebee/api_error.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
class APIError(Exception):

def __init__(self, http_code,json_obj):
def __init__(self, http_code,json_obj,headers):
Exception.__init__(self, json_obj.get('message'))
self.json_obj = json_obj
self.http_status_code = http_code
self.type = json_obj.get('type')
self.api_error_code = json_obj.get('api_error_code')
self.param = json_obj.get('param')

self.http_headers = headers

self.error_code = json_obj['error_code']
self.http_code = http_code
self.http_body = None

class PaymentError(APIError):
def __init__(self, http_code,json_obj):
APIError.__init__(self, http_code,json_obj)
def __init__(self, http_code,json_obj,headers):
APIError.__init__(self, http_code,json_obj,headers)

class InvalidRequestError(APIError):
def __init__(self, http_code,json_obj):
APIError.__init__(self, http_code,json_obj)
def __init__(self, http_code,json_obj,headers):
APIError.__init__(self, http_code,json_obj,headers)

class OperationFailedError(APIError):
def __init__(self, http_code,json_obj):
APIError.__init__(self, http_code,json_obj)
def __init__(self, http_code,json_obj,headers):
APIError.__init__(self, http_code,json_obj,headers)


16 changes: 8 additions & 8 deletions chargebee/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def request(method, url, env, params=None, headers=None):
method=request_args['method'], status_code=response.status_code, text=response.text.encode("utf 8")
))

return process_response(url, response.text, response.status_code)
return process_response(url, response.text, response.status_code, response.headers)


def process_response(url,response, http_code):
def process_response(url,response, http_code, response_headers):
try:
resp_json = compat.json.loads(response)
except Exception as ex:
Expand All @@ -93,22 +93,22 @@ def process_response(url,response, http_code):
raise Exception("Sorry, something went wrong when trying to process the request. If this problem persists, contact us at [email protected]. \n type: internal_error, \n http_status_code: 500, \n error_code: internal_error,\n content:" + response)

if http_code < 200 or http_code > 299:
handle_api_resp_error(url,http_code, resp_json)
handle_api_resp_error(url,http_code, resp_json, response_headers)

return resp_json


def handle_api_resp_error(url,http_code, resp_json):
def handle_api_resp_error(url,http_code, resp_json, response_headers):
if 'api_error_code' not in resp_json:
raise Exception("The api_error_code is not present. Probably not a chargebee error. \n URL is " + url + "\nContent is \n " + str(resp_json))

if 'payment' == resp_json.get('type'):
raise PaymentError(http_code, resp_json)
raise PaymentError(http_code, resp_json, response_headers)
elif 'operation_failed' == resp_json.get('type'):
raise OperationFailedError(http_code, resp_json)
raise OperationFailedError(http_code, resp_json, response_headers)
elif 'invalid_request' == resp_json.get('type'):
raise InvalidRequestError(http_code, resp_json)
raise InvalidRequestError(http_code, resp_json, response_headers)
else:
raise APIError(http_code, resp_json)
raise APIError(http_code, resp_json, response_headers)