Skip to content

excepthook

custom_excepthook(*args)

CLI support. Sets an exception hook, so that if an exception is raised, if it's a user exception, then the traceback will only be the inner cause, not the outer frames.

Source code in lineapy/exceptions/excepthook.py
39
40
41
42
43
44
45
def custom_excepthook(*args):
    """
    CLI support.
    Sets an exception hook, so that if an exception is raised, if it's a user
    exception, then the traceback will only be the inner cause, not the outer frames.
    """
    return sys.__excepthook__(*transform_except_hook_args(args))  # type: ignore

set_custom_excepthook()

To support CLI error reporting (or the repl, which we do not currently have).

Source code in lineapy/exceptions/excepthook.py
48
49
50
51
52
53
def set_custom_excepthook():
    """
    To support CLI error reporting (or the repl, which we do not currently have).
    """
    if REWRITE_EXCEPTIONS:
        sys.excepthook = custom_excepthook

transform_except_hook_args(args, *changes)

Used by both CLI and Jupyter to pull out the cause from a user exception and also apply the changes to the frames if it's a UserException, which is a custom exception that WE created.

changes is for Jupyter notebook support. They were discovered through trial and error with different type of executions, as seen in executor.py.

If the error is from lineapy, we keep the original frames.

Source code in lineapy/exceptions/excepthook.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def transform_except_hook_args(
    args: ExceptHookArgs, *changes: TracebackChange
) -> ExceptHookArgs:
    """
    Used by both CLI and Jupyter to pull out the cause from a user exception
    and also apply the changes to the frames if it's a `UserException`, which
    is a custom exception that WE created.

    `changes` is for Jupyter notebook support. They were discovered through trial and error with different type
    of executions, as seen in `executor.py`.

    If the error is from lineapy, we keep the original frames.
    """
    # the 2nd arg is the Exception (per the docs, and described in
    # `ExceptHookArgs`)
    exc_value = args[1]
    if isinstance(exc_value, UserException):
        # the user's exception
        cause = exc_value.__cause__
        apply_changes(cause, changes)
        return type(cause), cause, cause.__traceback__
    return args

Was this helpful?

Help us improve docs with your feedback!