diff --git a/python/common/error_middleware.py b/python/common/error_middleware.py index cc58e363..2754f245 100644 --- a/python/common/error_middleware.py +++ b/python/common/error_middleware.py @@ -1,5 +1,6 @@ # Error middleware functions import json +import traceback import logging import functools import inspect @@ -63,6 +64,12 @@ def record_error(error_code: ErrorCode, error_details, event_id: int = None, eve if not payload: payload = get_safe_payload() + + # Handle stack trace extraction if error_details is an exception + if isinstance(error_details, Exception): + stack_trace = ''.join(traceback.format_exception(type(error_details), error_details, error_details.__traceback__)) + else: + stack_trace = str(error_details) error_log = DFErrors( error_cd=str(error_code.code), @@ -75,7 +82,7 @@ def record_error(error_code: ErrorCode, error_details, event_id: int = None, eve event_type=str(event_type) if event_type else None, ticket_no=ticket_no, req_payload=payload, - error_details=error_details, + error_details=stack_trace, error_path=function_path, created_by='SYSTEM', received_dt=datetime.now(), diff --git a/python/common/models.py b/python/common/models.py index 8ea64f89..22a326ca 100644 --- a/python/common/models.py +++ b/python/common/models.py @@ -804,7 +804,7 @@ class DFErrors(db.Model): error_details: str = db.Column(db.Text) error_path: str = db.Column(db.String(200)) event_id: int = db.Column(db.Integer, db.ForeignKey('event.event_id'), nullable=True) - event_type: str = db.Column(db.String(10), nullable=True) + event_type: str = db.Column(db.String(30), nullable=True) ticket_no: str = db.Column(db.String(50), nullable=True) received_dt: datetime = db.Column(db.DateTime, default=datetime.now()) error_status_cd: str = db.Column(db.String(200), default=ErrorStatus.NEW) diff --git a/python/common/ride_actions.py b/python/common/ride_actions.py index 692fa1d0..b4fb092a 100644 --- a/python/common/ride_actions.py +++ b/python/common/ride_actions.py @@ -42,7 +42,7 @@ def twelve_hours_event(**args): if response.status_code != 200: args['error'] = { 'error_code': ErrorCode.R01, - 'error_details': 'error in sending 12hr_submitted event to RIDE', + 'error_details': f'Error in sending 12hr_submitted event to RIDE, Response code: {response.status_code} response text: {response.json()}', 'event_type': '12hr', 'func': twelve_hours_event, 'ticket_no': args['form_data']['twelve_hour_number'] @@ -57,7 +57,7 @@ def twelve_hours_event(**args): logging.error(e) args['error'] = { 'error_code': ErrorCode.R01, - 'error_details': str(e), + 'error_details': e, 'event_type': '12hr', 'func': twelve_hours_event, 'ticket_no': args['form_data']['twelve_hour_number'] @@ -105,7 +105,7 @@ def twenty_four_hours_event(**args): if response.status_code != 200: args['error'] = { 'error_code': ErrorCode.R01, - 'error_details': 'Error in sending 24hr_submitted event to RIDE', + 'error_details': f'Error in sending 24hr_submitted event to RIDE, Response code: {response.status_code} response text: {response.json()}', 'event_type': '24hr', 'func': twenty_four_hours_event, 'ticket_no': args['form_data']['twenty_four_hour_number'] @@ -120,7 +120,7 @@ def twenty_four_hours_event(**args): logging.error(e) args['error'] = { 'error_code': ErrorCode.R01, - 'error_details': str(e), + 'error_details': e, 'event_type': '24hr', 'func': twenty_four_hours_event, 'ticket_no': args['form_data']['twenty_four_hour_number'] @@ -178,7 +178,7 @@ def vi_event(**args): if response.status_code != 200: args['error'] = { 'error_code': ErrorCode.R01, - 'error_details': 'Error in sending vi_submitted event to RIDE', + 'error_details': f'Error in sending vi_submitted event to RIDE, Response code: {response.status_code} response text: {response.json()}', 'event_type': 'VI', 'func': vi_event, 'ticket_no': args['form_data']['VI_number'] @@ -193,7 +193,7 @@ def vi_event(**args): logging.error(e) args['error'] = { 'error_code': ErrorCode.R01, - 'error_details': str(e), + 'error_details': e, 'event_type': 'VI', 'func': vi_event, 'ticket_no': args['form_data']['VI_number'] diff --git a/python/form_handler/actions.py b/python/form_handler/actions.py index 31fbec84..a8cf52d7 100644 --- a/python/form_handler/actions.py +++ b/python/form_handler/actions.py @@ -199,11 +199,13 @@ def validate_event_retry_count(**args)->tuple: args['put_to_queue_name']=put_to_queue_name # Set error in args to get consumed by the record_event_error function + event_id = message.get('event_id', None) + event_type = message.get('event_type', None) args['error'] = { 'error_code': ErrorCode.E05, - 'error_details': 'Retry count exceeds for an event.', - 'event_id': message.get('event_id'), - 'event_type': message.get('event_type'), + 'error_details': f'Retry count exceeds for the event id: {event_id}, event_type: {event_type}', + 'event_id': event_id, + 'event_type': event_type, 'func': validate_event_retry_count, 'payload': message, } @@ -216,7 +218,7 @@ def validate_event_retry_count(**args)->tuple: message = args.get('message') args['error'] = { 'error_code': ErrorCode.E03, - 'error_details': str(e), + 'error_details': e, 'event_id': message.get('event_id'), 'event_type': message.get('event_type'), 'func': validate_event_retry_count, @@ -506,7 +508,7 @@ def prep_icbc_payload(**args)->tuple: logging.error(e) args['error'] = { 'error_code': ErrorCode.I02, - 'error_details': str(e), + 'error_details': e, 'event_id': message.get('event_id') if message else None, 'event_type': message.get('event_type') if message else None, 'func': prep_icbc_payload, @@ -530,7 +532,7 @@ def send_to_icbc(**args)->tuple: if send_status is False: args['error'] = { 'error_code': ErrorCode.I01, - 'error_details': 'Error in sending events to ICBC', + 'error_details': f'icbc_resp_code: {icbc_resp_code} icbc_response_txt: {icbc_response_txt}', 'event_id': message.get('event_id') if message else None, 'event_type': message.get('event_type') if message else None, 'func': send_to_icbc, @@ -541,7 +543,7 @@ def send_to_icbc(**args)->tuple: logging.error(e) args['error'] = { 'error_code': ErrorCode.I01, - 'error_details': str(e), + 'error_details': e, 'event_id': message.get('event_id') if message else None, 'event_type': message.get('event_type') if message else None, 'func': send_to_icbc, @@ -1070,7 +1072,7 @@ def update_event_status_hold(**args)->tuple: error_args = { 'error': { 'error_code': ErrorCode.E06, - 'error_details': f'Holding a {event_type} event', + 'error_details': f'Holding a event_id: {event_id}, event_type:{event_type}', 'event_id': event_id, 'event_type': event_type, 'func': update_event_status_hold, @@ -1085,7 +1087,7 @@ def update_event_status_hold(**args)->tuple: error_args = { 'error': { 'error_code': ErrorCode.E06, - 'error_details': f'Exception in update_event_status_hold: {str(e)}', + 'error_details': e, 'event_id': event_id, 'event_type': event_type, 'func': update_event_status_hold, @@ -1140,7 +1142,7 @@ def update_event_status_error(**args)->tuple: error_args = { 'error': { 'error_code': ErrorCode.E07, - 'error_details': f'Exception in update_event_status_error: {str(e)}', + 'error_details': e, 'event_id': event_id, 'event_type': event_type, 'func': update_event_status_error, @@ -1216,7 +1218,7 @@ def add_to_persistent_failed_queue(**args)->tuple: message = args.get('message') args['error'] = { 'error_code': ErrorCode.E03, - 'error_details': str(e), + 'error_details': e, 'event_id': message.get('event_id'), 'event_type': message.get('event_type'), 'func': add_to_persistent_failed_queue, diff --git a/python/prohibition_web_svc/middleware/event_middleware.py b/python/prohibition_web_svc/middleware/event_middleware.py index 43c59fd7..e2829cf9 100644 --- a/python/prohibition_web_svc/middleware/event_middleware.py +++ b/python/prohibition_web_svc/middleware/event_middleware.py @@ -248,7 +248,7 @@ def save_event_data(**kwargs) -> tuple: # Set error in kwargs to get consumed by the record_event_error function kwargs['error'] = { 'error_code': ErrorCode.E01, - 'error_details': str(e), + 'error_details': e, 'event_id': None, 'event_type': get_event_type(data), 'ticket_no': get_ticket_no(data), @@ -382,7 +382,7 @@ def save_event_pdf(**kwargs) -> tuple: # Set error in kwargs to get consumed by the record_event_error function kwargs['error'] = { 'error_code': ErrorCode.E02, - 'error_details': str(e), + 'error_details': e, 'event_id': event.event_id, 'event_type': get_event_type(data), 'ticket_no': get_ticket_no(data), diff --git a/python/prohibition_web_svc/middleware/form_middleware.py b/python/prohibition_web_svc/middleware/form_middleware.py index dbb95fca..0eb8c497 100644 --- a/python/prohibition_web_svc/middleware/form_middleware.py +++ b/python/prohibition_web_svc/middleware/form_middleware.py @@ -53,7 +53,7 @@ def lease_a_form_id(**kwargs) -> tuple: except Exception as e: kwargs['error'] = { 'error_code': ErrorCode.F01, - 'error_details': str(e), + 'error_details': e, 'event_type': kwargs.get('form_type'), 'func': lease_a_form_id, } @@ -92,7 +92,7 @@ def renew_form_id_lease(**kwargs) -> tuple: except Exception as e: kwargs['error'] = { 'error_code': ErrorCode.F02, - 'error_details': str(e), + 'error_details': e, 'event_type': kwargs.get('form_type'), 'func': renew_form_id_lease, } @@ -265,7 +265,7 @@ def admin_create_form(**kwargs) -> tuple: logging.warning(str(e)) kwargs['error'] = { 'error_code': ErrorCode.F02, - 'error_details': str(e), + 'error_details': e, 'event_type': kwargs.get('form_type'), 'func': renew_form_id_lease, } diff --git a/python/prohibition_web_svc/migrations/versions/211a0fba4348_.py b/python/prohibition_web_svc/migrations/versions/211a0fba4348_.py index 42a83878..2dcb0154 100644 --- a/python/prohibition_web_svc/migrations/versions/211a0fba4348_.py +++ b/python/prohibition_web_svc/migrations/versions/211a0fba4348_.py @@ -28,7 +28,7 @@ def upgrade(): sa.Column('error_details', sa.Text(), nullable=True), sa.Column('error_path', sa.String(length=200), nullable=True), sa.Column('event_id', sa.Integer(), nullable=True), - sa.Column('event_type', sa.String(length=10), nullable=True), + sa.Column('event_type', sa.String(length=30), nullable=True), sa.Column('ticket_no', sa.String(length=50), nullable=True), sa.Column('received_dt', sa.DateTime(), nullable=True), sa.Column('error_status_cd', sa.String(length=200), nullable=True),