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

feature: support of error parser for expense report export #113

Merged
merged 26 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 10 additions & 1 deletion netsuitesdk/api/expense_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import logging

from netsuitesdk.internal.utils import PaginatedSearch
from netsuitesdk.internal.exceptions import NetSuiteRequestError
from netsuitesdk.errors.parser import expense_report_error_parser
from netsuitesdk.errors.helpers import export_error_matcher

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,5 +100,11 @@ def post(self, data) -> OrderedDict:
er['entity'] = self.ns_client.RecordRef(**(data['entity']))

logger.debug('able to create er = %s', er)
res = self.ns_client.upsert(er)
try:
res = self.ns_client.upsert(er)
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should use broad exception

error_dict = export_error_matcher(e.message, 'expense_report')
message = expense_report_error_parser(error_dict, self.ns_client, e.message)
raise NetSuiteRequestError(message, e.code)

return self._serialize(res)
Empty file added netsuitesdk/errors/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions netsuitesdk/errors/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

error_reference = {
"expense_report":
{
"category_reference_error": r"An error occured in a upsert request: Invalid category reference key \d+ for entity \d+",
"account_reference_error": r"An error occured in a upsert request: Invalid account reference key \d+ for subsidiary \d+",
"project_reference_error": r"An error occured in a upsert request: Invalid customer reference key \d+ for entity \d+",
"location_reference_error": r"An error occured in a upsert request: Invalid location reference key \d+ for subsidiary \d+",
"department_reference_error": r"An error occured in a upsert request: Invalid department reference key \d+ for subsidiary \d+",
"currency_reference_error": r"An error occured in a upsert request: Invalid currency reference key \d+ for subsidiary \d+",
}
}
42 changes: 42 additions & 0 deletions netsuitesdk/errors/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import re
from .errors import error_reference


def decode_project_or_customer_name(name):
value = name.replace(u'\xa0', ' ')
value = value.replace('/', '-')
return value


def replace_numbers(string , replacement1, replacement2, number1, number2):
replaced_string = re.sub(r'\b({}|{})\b'.format(number1, number2), lambda m: replacement1 if m.group() == number1 else replacement2, string)
return replaced_string


def export_error_matcher(string, export_type):

if re.match(error_reference[export_type]['category_reference_error'], string):
numbers = re.findall(r'\d+', string)
return {"category": numbers[0], "entity": numbers[1]}

if re.match(error_reference[export_type]['account_reference_error'], string):
numbers = re.findall(r'\d+', string)
return {"account": numbers[0], "subsidiary": numbers[1]}

if re.match(error_reference[export_type]['project_reference_error'], string):
numbers = re.findall(r'\d+', string)
return {"customer": numbers[0], "entity": numbers[1]}

if re.match(error_reference[export_type]['location_reference_error'], string):
numbers = re.findall(r'\d+', string)
return {"location": numbers[0], "subsidiary": numbers[1]}

if re.match(error_reference[export_type]['department_reference_error'], string):
numbers = re.findall(r'\d+', string)
return {"department": numbers[0], "subsidiary": numbers[1]}

if re.match(error_reference[export_type]['currency_reference_error'], string):
numbers = re.findall(r'\d+', string)
return {"currency": numbers[0], "subsidiary": numbers[1]}

return {}
39 changes: 39 additions & 0 deletions netsuitesdk/errors/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from .helpers import decode_project_or_customer_name, replace_numbers


def expense_report_error_parser(error_dict, ns_client, message):
Copy link
Contributor Author

@NileshPant1999 NileshPant1999 May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once we move ahead with other module and see the pattern
will make this function generic too.


if list(error_dict) == ['category', 'entity']:
category = ns_client.get('ExpenseCategory', error_dict['category'])['name']
entity = ns_client.get('Employee', error_dict['entity'])
entity_name = entity['email'] if entity['email'] else entity['firstName'] + " " + entity['lastName']
message = replace_numbers(message, category, entity_name, error_dict['category'], error_dict['entity'])

elif list(error_dict) == ['account', 'subsidiary']:
account = ns_client.get('Account', error_dict['account'])['acctName']
subsdiary = ns_client.get('Subsidiary', error_dict['subsidiary'])['name']
message = replace_numbers(message, account, subsdiary, error_dict['account'], error_dict['subsidiary'])

elif list(error_dict) == ['customer', 'entity']:
customer = ns_client.get('Customer', error_dict['customer'])['entityId']
customer_name = decode_project_or_customer_name(customer)
entity = ns_client.get('Employee', error_dict['entity'])
entity_name = entity['email'] if entity['email'] else entity['firstName'] + " " + entity['lastName']
message = replace_numbers(message, customer_name, entity_name, error_dict['customer'], error_dict['entity'])

elif list(error_dict) == ['location', 'subsidiary']:
location = ns_client.get('Location', error_dict['location'])['name']
subsdiary = ns_client.get('Subsidiary', error_dict['subsidiary'])['name']
message = replace_numbers(message, location, subsdiary, error_dict['location'], error_dict['subsidiary'])

elif list(error_dict) == ['department', 'subsidiary']:
department = ns_client.get('Department', error_dict['department'])['name']
subsdiary = ns_client.get('Subsidiary', error_dict['subsidiary'])['name']
message = replace_numbers(message, department, subsdiary, error_dict['department'], error_dict['subsidiary'])

elif list(error_dict) == ['currency', 'subsidiary']:
currency = ns_client.get('Currency', error_dict['currency'])['name']
subsdiary = ns_client.get('Subsidiary', error_dict['subsidiary'])['name']
message = replace_numbers(message, currency, subsdiary, error_dict['currency'], error_dict['subsidiary'])

return message