Skip to content

viat.exceptions

Exceptions (errors and warnings) common to all modules.

ValidationExceptionMixin

Bases: Exception

Mixin for exceptions related to validation.

Source code in src/viat/exceptions.py
class ValidationExceptionMixin(Exception):
    """Mixin for exceptions related to validation."""
    __cause__: BaseException | None

    def get_human_readable_string(self) -> str:
        """Print the reason for which a validation has failed.

        This is usually not a [`ViatException`](..ViatException) subclass,
        so the default implementation would ignore the cause.
        """
        if self.__cause__:
            return f'{self}: {self.__cause__}.'

        return f'{self}.'

get_human_readable_string()

Print the reason for which a validation has failed.

This is usually not a ViatException subclass, so the default implementation would ignore the cause.

Source code in src/viat/exceptions.py
def get_human_readable_string(self) -> str:
    """Print the reason for which a validation has failed.

    This is usually not a [`ViatException`](..ViatException) subclass,
    so the default implementation would ignore the cause.
    """
    if self.__cause__:
        return f'{self}: {self.__cause__}.'

    return f'{self}.'

ViatAttributeStorageError

Bases: ViatError

Generic error class for storage-related issues.

Source code in src/viat/exceptions.py
class ViatAttributeStorageError(ViatError):
    """Generic error class for storage-related issues."""

ViatAttributeStorageWarning

Bases: ViatWarning

Generic warning class for storage-related issues.

Source code in src/viat/exceptions.py
class ViatAttributeStorageWarning(ViatWarning):
    """Generic warning class for storage-related issues."""

ViatCliError

Bases: ViatError

Error class for the command-line interface.

Source code in src/viat/exceptions.py
class ViatCliError(ViatError):
    """Error class for the command-line interface."""

ViatConfigError

Bases: ViatVaultError

Generic error class for configurations.

Source code in src/viat/exceptions.py
class ViatConfigError(ViatVaultError):
    """Generic error class for configurations."""

ViatError

Bases: ViatException

Generic error class.

Source code in src/viat/exceptions.py
class ViatError(ViatException):
    """Generic error class."""

ViatException

Bases: Exception

Generic exception class.

Source code in src/viat/exceptions.py
class ViatException(Exception):
    """Generic exception class."""

    def get_human_readable_string(self) -> str:
        """Get a human readable string suitable for e.g. logging."""
        if isinstance(self.__cause__, ViatException):
            return f'{self}. {self.__cause__}.'

        return f'{self}.'

get_human_readable_string()

Get a human readable string suitable for e.g. logging.

Source code in src/viat/exceptions.py
def get_human_readable_string(self) -> str:
    """Get a human readable string suitable for e.g. logging."""
    if isinstance(self.__cause__, ViatException):
        return f'{self}. {self.__cause__}.'

    return f'{self}.'

ViatFileTrackerError

Bases: ViatError

Generic error class for trackers.

Source code in src/viat/exceptions.py
class ViatFileTrackerError(ViatError):
    """Generic error class for trackers."""

ViatIntegrityError

Bases: ViatError

Error class for internal errors.

Source code in src/viat/exceptions.py
class ViatIntegrityError(ViatError):
    """Error class for internal errors."""

ViatMalformedStoredDataError

Bases: ValidationExceptionMixin, ViatAttributeStorageError

Error class for malformed data.

Source code in src/viat/exceptions.py
class ViatMalformedStoredDataError(ValidationExceptionMixin, ViatAttributeStorageError):
    """Error class for malformed data."""

    def get_path(self) -> pathlib.Path:
        """Get the path used to initialize the error."""
        return self.args[0]

    def __str__(self) -> str:
        return f'Malformed data stored for {self.get_path()}'

get_path()

Get the path used to initialize the error.

Source code in src/viat/exceptions.py
def get_path(self) -> pathlib.Path:
    """Get the path used to initialize the error."""
    return self.args[0]

ViatMissingAttributeError

Bases: ViatAttributeStorageError, KeyError

Error class for missing attributes.

Parameters:

  • path (Path) –

    Path for which the attribute is missing.

  • attr (str) –

    Name of missing attribute.

Source code in src/viat/exceptions.py
class ViatMissingAttributeError(ViatAttributeStorageError, KeyError):
    """Error class for missing attributes.

    Args:
        path: Path for which the attribute is missing.
        attr: Name of missing attribute.
    """

    def __init__(self, path: pathlib.Path, attr: str) -> None:
        super().__init__(path, attr)

    def get_path(self) -> pathlib.Path:
        """Get the path used to initialize the error."""
        return self.args[0]

    def get_attr(self) -> pathlib.Path:
        """Get the attribute name used to initialize the error."""
        return self.args[1]

    def __str__(self) -> str:
        return f'Attribute {self.get_attr()!r} has not been set for {self.get_path()}'

get_attr()

Get the attribute name used to initialize the error.

Source code in src/viat/exceptions.py
def get_attr(self) -> pathlib.Path:
    """Get the attribute name used to initialize the error."""
    return self.args[1]

get_path()

Get the path used to initialize the error.

Source code in src/viat/exceptions.py
def get_path(self) -> pathlib.Path:
    """Get the path used to initialize the error."""
    return self.args[0]

ViatStoredDataValidationWarning

Bases: ValidationExceptionMixin, ViatAttributeStorageWarning

Warning class for stored data that does not pass validation.

Parameters:

  • path (Path) –

    Path for which the stored data is not valid.

Source code in src/viat/exceptions.py
class ViatStoredDataValidationWarning(ValidationExceptionMixin, ViatAttributeStorageWarning):
    """Warning class for stored data that does not pass validation.

    Args:
        path: Path for which the stored data is not valid.
    """

    def __init__(self, path: pathlib.Path, cause: BaseException | None = None) -> None:
        super().__init__(path)
        self.__cause__ = cause

    def get_path(self) -> pathlib.Path:
        """Get the path used to initialize the error."""
        return self.args[0]

    def __str__(self) -> str:
        return f'Validation error in stored data for {self.get_path()}'

get_path()

Get the path used to initialize the error.

Source code in src/viat/exceptions.py
def get_path(self) -> pathlib.Path:
    """Get the path used to initialize the error."""
    return self.args[0]

ViatUntrackedFileWarning

Bases: ViatAttributeStorageWarning

Warning class for untracked files.

Source code in src/viat/exceptions.py
class ViatUntrackedFileWarning(ViatAttributeStorageWarning):
    """Warning class for untracked files."""

    def get_path(self) -> pathlib.Path:
        """Get the path used to initialize the error."""
        return self.args[0]

    def __str__(self) -> str:
        return f'File {self.get_path()} is not being tracked'

get_path()

Get the path used to initialize the error.

Source code in src/viat/exceptions.py
def get_path(self) -> pathlib.Path:
    """Get the path used to initialize the error."""
    return self.args[0]

ViatValidationError

Bases: ValidationExceptionMixin, ViatAttributeStorageError

Error class for schema validation failure.

Parameters:

  • path (Path) –

    Path for which the schema is not valid.

Source code in src/viat/exceptions.py
class ViatValidationError(ValidationExceptionMixin, ViatAttributeStorageError):
    """Error class for schema validation failure.

    Args:
        path: Path for which the schema is not valid.
    """

    def __init__(self, path: pathlib.Path) -> None:
        super().__init__(path)

    def get_path(self) -> pathlib.Path:
        """Get the path used to initialize the error."""
        return self.args[0]

    def __str__(self) -> str:
        return f'Validation error for {self.get_path()}'

get_path()

Get the path used to initialize the error.

Source code in src/viat/exceptions.py
def get_path(self) -> pathlib.Path:
    """Get the path used to initialize the error."""
    return self.args[0]

ViatVaultError

Bases: ViatError

Generic error class for configurations.

Source code in src/viat/exceptions.py
class ViatVaultError(ViatError):
    """Generic error class for configurations."""

ViatWarning

Bases: ViatException, Warning

Generic warnings class.

Source code in src/viat/exceptions.py
class ViatWarning(ViatException, Warning):
    """Generic warnings class."""

emit_warning(warning, stacklevel)

A wrapper around warnings.warn that processes intermediate handlers.

If any of the handlers return True, the next ones, including warnings.warn, are not processed.

Parameters:

  • warning (ViatWarning) –

    The warning to emit.

  • stacklevel (int) –

    A properly incremented stack level passed to the handlers.

Source code in src/viat/exceptions.py
def emit_warning(warning: ViatWarning, stacklevel: int) -> None:
    """A wrapper around [`warnings.warn`][] that processes intermediate handlers.

    If any of the handlers return `True`, the next ones, including [`warnings.warn`][], are not processed.

    Args:
        warning: The warning to emit.
        stacklevel: A properly incremented stack level passed to the handlers.
    """
    for handler in _warning_handlers:
        if handler(warning, stacklevel + 2):
            return

    warnings.warn(warning, stacklevel=stacklevel + 2)

install_warning_handler(handler)

Install a new warning handler.

Parameters:

  • handler (Callable[[ViatWarning, int], bool | None]) –

    A function that accepts a warning and stacklevel.

    If its return value is True, we do not process the next handlers.

Source code in src/viat/exceptions.py
def install_warning_handler(handler: Callable[[ViatWarning, int], bool | None]) -> None:
    """Install a new warning handler.

    Args:
        handler: A function that accepts a warning and stacklevel.

            If its return value is `True`, we do not process the next handlers.
    """
    _warning_handlers.append(handler)

with_warning_handler(handler)

Setup a warning handler in a context manager.

Parameters:

Source code in src/viat/exceptions.py
@contextlib.contextmanager
def with_warning_handler(handler: Callable[[ViatWarning, int], bool | None]) -> Generator[None]:
    """Setup a warning handler in a context manager.

    Args:
        handler: A function that accepts a warning and stacklevel.
    """
    _warning_handlers.append(handler)

    try:
        yield
    finally:
        index = _warning_handlers.index(handler)
        del _warning_handlers[index]