Skip to content

Repository Errors

repository_errors

Error classes for repository-level storage operations.

Provides structured error types for save failures, deletion of
non-existent aggregates, and retrieval errors.

RepositoryError

Bases: RuntimeErrorMixin, Error[MetadataValueType]

Generic error raised when a repository operation fails.

This is the base error for all repository-level failures. Concrete
implementations may raise this or more specific subclasses.

Example
class ErrorMessage:
    def __init__(self, value: str) -> None:
        self.value = value


error = RepositoryError(ErrorMessage("Save failed"))
Source code in src/forging_blocks/infrastructure/errors/repository_errors.py
class RepositoryError[MetadataValueType = object](RuntimeErrorMixin, Error[MetadataValueType]):
    """Generic error raised when a repository operation fails.

    This is the base error for all repository-level failures. Concrete
    implementations may raise this or more specific subclasses.

    Example:
        ```python
        class ErrorMessage:
            def __init__(self, value: str) -> None:
                self.value = value


        error = RepositoryError(ErrorMessage("Save failed"))
        ```
    """

RepositoryNotFoundError

Bases: RepositoryError[MetadataValueType]

Error raised when attempting to delete or retrieve an aggregate that does not exist.

Example
try:
    raise RepositoryNotFoundError.for_id("user-42")
except RepositoryNotFoundError as exc:
    print(exc.message)
Source code in src/forging_blocks/infrastructure/errors/repository_errors.py
class RepositoryNotFoundError[MetadataValueType = object](RepositoryError[MetadataValueType]):
    """Error raised when attempting to delete or retrieve an aggregate that does not exist.

    Example:
        ```python
        try:
            raise RepositoryNotFoundError.for_id("user-42")
        except RepositoryNotFoundError as exc:
            print(exc.message)
        ```

    """

    @classmethod
    def for_id(cls, entity_id: object) -> "RepositoryNotFoundError[MetadataValueType]":
        """Create an error for a specific missing aggregate ID.

        Args:
            entity_id: The identifier that was not found.

        Returns:
            A RepositoryNotFoundError with a descriptive message.

        """
        return cls(ErrorMessage(f"Aggregate with id '{entity_id}' not found."))

for_id(entity_id: object) -> RepositoryNotFoundError[MetadataValueType] classmethod

Create an error for a specific missing aggregate ID.

Parameters:

Name Type Description Default
entity_id object

The identifier that was not found.

required

Returns:

Type Description
RepositoryNotFoundError[MetadataValueType]

A RepositoryNotFoundError with a descriptive message.

Source code in src/forging_blocks/infrastructure/errors/repository_errors.py
@classmethod
def for_id(cls, entity_id: object) -> "RepositoryNotFoundError[MetadataValueType]":
    """Create an error for a specific missing aggregate ID.

    Args:
        entity_id: The identifier that was not found.

    Returns:
        A RepositoryNotFoundError with a descriptive message.

    """
    return cls(ErrorMessage(f"Aggregate with id '{entity_id}' not found."))