From 3a8f78a0e591befd4b4ed3135d2ee497330fb453 Mon Sep 17 00:00:00 2001 From: Brian Vanderford Date: Wed, 5 Apr 2023 17:16:14 -0400 Subject: [PATCH] Include response headers in exceptions --- chargebee/api_error.py | 16 ++++++++-------- chargebee/http_request.py | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/chargebee/api_error.py b/chargebee/api_error.py index b7c99d6..11ab9d0 100644 --- a/chargebee/api_error.py +++ b/chargebee/api_error.py @@ -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) diff --git a/chargebee/http_request.py b/chargebee/http_request.py index 146dbab..dd944f8 100644 --- a/chargebee/http_request.py +++ b/chargebee/http_request.py @@ -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: @@ -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 support@chargebee.com. \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)