Skip to content

UncaughtExceptionWatcher

The UncaughtExceptionWatcher monitors uncaught exceptions in the command/function. It can be created using the capsula.UncaughtExceptionWatcher.__init__ method.

capsula.UncaughtExceptionWatcher.__init__

__init__(
    name: str = "exception",
    *,
    base: type[BaseException] = Exception
)
PARAMETER DESCRIPTION
name

Name of the exception. Used as a key in the output.

TYPE: str DEFAULT: 'exception'

base

Base exception class to catch.

TYPE: type[BaseException] DEFAULT: Exception

Source code in capsula/_watcher/_exception.py
def __init__(
    self,
    name: Annotated[str, Doc("Name of the exception. Used as a key in the output.")] = "exception",
    *,
    base: Annotated[type[BaseException], Doc("Base exception class to catch.")] = Exception,
) -> None:
    self._name = name
    self._base = base
    self._exception: BaseException | None = None

Configuration example

Via capsula.toml

[in-run]
watchers = [
  { type = "UncaughtExceptionWatcher" },
]

Via @capsula.watcher decorator

import capsula

@capsula.run()
@capsula.watcher(capsula.UncaughtExceptionWatcher("exception"))
def func(): ...

Output example

The following is an example of the output of the UncaughtExceptionWatcher, reported by the JsonDumpReporter:

"exception": {
  "exception": {
    "exc_type": null,
    "exc_value": null,
    "traceback": null
  }
}

If an exception is caught, the following is an example of the output:

"exception": {
  "exception": {
    "exc_type": "ZeroDivisionError",
    "exc_value": "float division by zero",
    "traceback": "  File \"/home/nomura/ghq/github.com/shunichironomura/capsula/capsula/_run.py\", line 288, in __call__\n    result = self._func(*args, **kwargs)\n  File \"examples/simple_decorator.py\", line 21, in calculate_pi\n    x = 10.0 / 0.0\n"
  }
}