Skip to content

Class swarmauri_standard.exceptions.IndexErrorWithContext.IndexErrorWithContext

swarmauri_standard.exceptions.IndexErrorWithContext.IndexErrorWithContext

IndexErrorWithContext(original_exception)

Bases: Exception

Source code in swarmauri_standard/exceptions/IndexErrorWithContext.py
5
6
7
8
def __init__(self, original_exception):
    self.original_exception = original_exception
    self.stack_info = inspect.stack()
    self.handle_error()

original_exception instance-attribute

original_exception = original_exception

stack_info instance-attribute

stack_info = stack()

handle_error

handle_error()
Source code in swarmauri_standard/exceptions/IndexErrorWithContext.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def handle_error(self):
    # You might want to log this information or handle it differently depending on your application's needs
    frame = self.stack_info[
        1
    ]  # Assuming the IndexError occurs one level up from where it's caught
    error_details = {
        "message": str(self.original_exception),
        "function": frame.function,
        "file": frame.filename,
        "line": frame.lineno,
        "code_context": "".join(frame.code_context).strip()
        if frame.code_context
        else "No context available",
    }
    print("IndexError occurred with detailed context:")
    for key, value in error_details.items():
        print(f"{key.capitalize()}: {value}")