Skip to content

Commit

Permalink
fix json decode error (#558)
Browse files Browse the repository at this point in the history
* fix json decode error

* refactor json-decoder
  • Loading branch information
Hrishabh17 authored May 2, 2024
1 parent 1104d20 commit 0d67e3f
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions apps/netsuite/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@ def __decode_project_or_customer_name(name):

@staticmethod
def get_message_and_code(raw_response):
logger.info('Charge Card Error - %s', raw_response.text)
try:
response = json.loads(eval(raw_response.text))
except Exception:
response = json.loads((raw_response.text.replace('"{', '{').replace('}"', '}').replace('\\', '').replace('"https://', "'https://").replace('.html"', ".html'")))
return parse_error_and_get_message(raw_response=raw_response.text, get_code=True)
except Exception as e:
logger.info('Error while parsing error message - %s', e)
raise

logger.info('Charge Card Error - %s', response)
code = response['error']['code'] if 'error' in response and 'code' in response['error'] else response['code']
message = response['error']['message']['message'] if 'error' in response and 'message' in response['error'] and 'message' in response['error']['message'] else response['message']['message']

return code, message

@staticmethod
def get_tax_code_name(item_id, tax_type, rate):
if tax_type:
Expand Down Expand Up @@ -2140,7 +2136,7 @@ def post_vendor_payment(self, vendor_payment: VendorPayment,
return created_vendor_payment


def parse_error_and_get_message(raw_response):
def parse_error_and_get_message(raw_response, get_code: bool = False):
try:
if raw_response == '<HTML><HEAD>' or raw_response == '<html>':
return 'HTML bad response from NetSuite'
Expand All @@ -2149,6 +2145,8 @@ def parse_error_and_get_message(raw_response):
.replace("True", 'true')\
.replace("None", 'null')
parsed_response = json.loads(raw_response)
if get_code:
return get_message_and_code(parsed_response)
return get_message_from_parsed_error(parsed_response)
except Exception:
raw_response = raw_response.replace('"creditCardCharge"', 'creditCardCharge')\
Expand All @@ -2158,6 +2156,8 @@ def parse_error_and_get_message(raw_response):
.replace('"https://', "'https://").replace('.html"', ".html'")\
.replace('="', "=").replace('">', ">")
parsed_response = json.loads(raw_response)
if get_code:
return get_message_and_code(parsed_response)
return get_message_from_parsed_error(parsed_response)


Expand All @@ -2173,3 +2173,9 @@ def get_message_from_parsed_error(parsed_response):
return parsed_response['message']['message']
except Exception:
raise


def get_message_and_code(parsed_response):
message = get_message_from_parsed_error(parsed_response)
code = parsed_response['error']['code'] if 'error' in parsed_response and 'code' in parsed_response['error'] else parsed_response['code']
return code, message

0 comments on commit 0d67e3f

Please sign in to comment.