Spaces:
Sleeping
Sleeping
| import sys | |
| import logging | |
| def error_message_detail(error: Exception, error_detail: sys) -> str: | |
| _, _, exc_tb = error_detail.exc_info() | |
| if exc_tb is not None: | |
| # Get the file name where the exception occurred | |
| file_name = exc_tb.tb_frame.f_code.co_filename | |
| line_number = exc_tb.tb_lineno | |
| else: | |
| # Fallback to current frame info if no traceback (e.g. manual raise) | |
| import inspect | |
| frame = inspect.currentframe().f_back.f_back # Go back to where MyException was called | |
| file_name = frame.f_code.co_filename | |
| line_number = frame.f_lineno | |
| # Create a formatted error message string with file name, line number, and the actual error | |
| error_message = f"Error occurred in python script: [{file_name}] at line number [{line_number}]: {str(error)}" | |
| # Log the error for better tracking | |
| logging.error(error_message) | |
| return error_message | |
| class MyException(Exception): | |
| def __init__(self, error_message: str, error_detail: sys): | |
| # Call the base class constructor with the error message | |
| super().__init__(error_message) | |
| # Format the detailed error message using the error_message_detail function | |
| self.error_message = error_message_detail(error_message, error_detail) | |
| def __str__(self) -> str: | |
| """ | |
| Returns the string representation of the error message. | |
| """ | |
| return self.error_message |