Skip to content

Commit

Permalink
Change the depth of error_details to store the full stack trace and m…
Browse files Browse the repository at this point in the history
…ore details
  • Loading branch information
ratheesh-aot committed Sep 26, 2024
1 parent 953c61b commit b07b1f3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 25 deletions.
9 changes: 8 additions & 1 deletion python/common/error_middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Error middleware functions
import json
import traceback
import logging
import functools
import inspect
Expand Down Expand Up @@ -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),
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion python/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions python/common/ride_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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']
Expand Down Expand Up @@ -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']
Expand All @@ -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']
Expand Down Expand Up @@ -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']
Expand All @@ -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']
Expand Down
24 changes: 13 additions & 11 deletions python/form_handler/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions python/prohibition_web_svc/middleware/event_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions python/prohibition_web_svc/middleware/form_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit b07b1f3

Please sign in to comment.