File size: 1,755 Bytes
c45cf35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""

This module defines a custom exception handling class and a function to get error message with details of the error.

"""

# Standard Library
import sys

# Local imports
from src.logger import logging


# Function Definition to get error message with details of the error (file name and line number) when an error occurs in the program
def get_error_message(error, error_detail: sys):
    """

    Get error message with details of the error.



    Args:

        - error (Exception): The error that occurred.

        - error_detail (sys): The details of the error.



    Returns:

        str: A string containing the error message along with the file name and line number where the error occurred.

    """
    _, _, exc_tb = error_detail.exc_info()

    # Get error details
    file_name = exc_tb.tb_frame.f_code.co_filename
    return "Error occured in python script name [{0}] line number [{1}] error message[{2}]".format(
        file_name, exc_tb.tb_lineno, str(error)
    )


# Custom Exception Handling Class Definition
class CustomExceptionHandling(Exception):
    """

    Custom Exception Handling:

        This class defines a custom exception that can be raised when an error occurs in the program.

        It takes an error message and an error detail as input and returns a formatted error message when the exception is raised.

    """

    # Constructor
    def __init__(self, error_message, error_detail: sys):
        """Initialize the exception"""
        super().__init__(error_message)

        self.error_message = get_error_message(error_message, error_detail=error_detail)

    def __str__(self):
        """String representation of the exception"""
        return self.error_message