From 554ce04e0df3292669dc6bc6cafe339a1e4351c7 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Mon, 14 Dec 2020 18:59:01 +0530 Subject: [PATCH] fix: error handling --- erpnext/regional/india/e_invoice/utils.py | 80 ++++++++++++++++------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/erpnext/regional/india/e_invoice/utils.py b/erpnext/regional/india/e_invoice/utils.py index ed7187846c7..dc932a15e24 100644 --- a/erpnext/regional/india/e_invoice/utils.py +++ b/erpnext/regional/india/e_invoice/utils.py @@ -308,14 +308,17 @@ def make_einvoice(invoice): "Errors: ", json.dumps(errors, indent=4) ]) frappe.log_error(title="E Invoice Validation Failed", message=message) - if len(errors) > 1: - li = ['
  • '+ d +'
  • ' for d in errors] - frappe.throw("".format(''.join(li)), title=_('E Invoice Validation Failed')) - else: - frappe.throw(errors[0], title=_('E Invoice Validation Failed')) + throw_error_list(errors, _('E Invoice Validation Failed')) return einvoice +def throw_error_list(errors, title): + if len(errors) > 1: + li = ['
  • '+ d +'
  • ' for d in errors] + frappe.throw("".format(''.join(li)), title=title) + else: + frappe.throw(errors[0], title=title) + def validate_einvoice(validations, einvoice, errors=[]): for fieldname, field_validation in validations.items(): value = einvoice.get(fieldname, None) @@ -406,6 +409,7 @@ class GSPConnector(): "response": json.dumps(res, indent=4) if res else None }) request_log.insert(ignore_permissions=True) + frappe.db.commit() def fetch_auth_token(self): headers = { @@ -487,11 +491,11 @@ class GSPConnector(): Contact ERPNext support to resolve the issue.') else: - self.log_error(res) raise RequestFailed except RequestFailed: - self.raise_error() + errors = self.sanitize_error_message(res.get('message')) + self.raise_error(errors=errors) except Exception: self.log_error(data) @@ -506,11 +510,11 @@ class GSPConnector(): if res.get('success'): return res.get('result') else: - self.log_error(res) raise RequestFailed except RequestFailed: - self.raise_error() + errors = self.sanitize_error_message(res.get('message')) + self.raise_error(errors=errors) except Exception: self.log_error() @@ -536,11 +540,11 @@ class GSPConnector(): self.update_invoice() else: - self.log_error(res) raise RequestFailed except RequestFailed: - self.raise_error() + errors = self.sanitize_error_message(res.get('message')) + self.raise_error(errors=errors) except Exception: self.log_error(data) @@ -577,11 +581,11 @@ class GSPConnector(): self.update_invoice() else: - self.log_error(res) raise RequestFailed except RequestFailed: - self.raise_error() + errors = self.sanitize_error_message(res.get('message')) + self.raise_error(errors=errors) except Exception: self.log_error(data) @@ -608,15 +612,37 @@ class GSPConnector(): self.update_invoice() else: - self.log_error(res) raise RequestFailed except RequestFailed: - self.raise_error() + errors = self.sanitize_error_message(res.get('message')) + self.raise_error(errors=errors) except Exception: self.log_error(data) self.raise_error(True) + + def sanitize_error_message(self, message): + ''' + message = '2174 : For inter-state transaction, CGST and SGST amounts are not applicable; only IGST amount is applicable, + 3095 : Supplier GSTIN is inactive' + we search for string between ':' to extract error messages + errors = [ + ': For inter-state transaction, CGST and SGST amounts are not applicable; only IGST amount is applicable, 3095 ', + ': Test' + ] + then we trim down the message by looping over errors + ''' + errors = re.findall(': [^:]+', message) + for idx, e in enumerate(errors): + # remove colons + errors[idx] = errors[idx].replace(':', '').strip() + # if not last + if idx != len(errors) - 1: + # remove last 7 chars eg: ', 3095 ' + errors[idx] = errors[idx][:-6] + + return errors def log_error(self, data={}): if not isinstance(data, dict): @@ -625,23 +651,27 @@ class GSPConnector(): seperator = "--" * 50 err_tb = traceback.format_exc() err_msg = str(sys.exc_info()[1]) - data = json.dumps(data, default=str, indent=4) + data = json.dumps(data, indent=4) message = "\n".join([ "Error", err_msg, seperator, "Data:", data, seperator, "Exception:", err_tb ]) - frappe.log_error(title="E Invoicing Error", message=message) + frappe.log_error(title=_('E Invoice Request Failed'), message=message) - def raise_error(self, raise_exception=False): - link_to_error_list = 'Error Log' - frappe.msgprint( - _('An error occurred while making e-invoicing request. Please check {} for more information.').format(link_to_error_list), - title=_('E Invoice Request Failed'), - raise_exception=raise_exception, - indicator='red' - ) + def raise_error(self, raise_exception=False, errors=[]): + title = _('E Invoice Request Failed') + if errors: + throw_error_list(errors, title) + else: + link_to_error_list = 'Error Log' + frappe.msgprint( + _('An error occurred while making e-invoicing request. Please check {} for more information.').format(link_to_error_list), + title=title, + raise_exception=raise_exception, + indicator='red' + ) def set_einvoice_data(self, res): enc_signed_invoice = res.get('SignedInvoice')