dodil/erp-brick-inventory

DODIL ERP brick: inventory

src/brick_inventory/domain/errors.py

"""The refusals a rule makes. The use cases turn them into a status with these words, through both doors."""


class RuleBroken(Exception):
    """A business rule said no (409). The message is written for the person who tried."""


class BadInput(RuleBroken):
    """A value outside its bounds or in the wrong shape (422): never an error from the engine."""


class GateBroken(RuleBroken):
    """A readiness gate is unmet (409): it names the gate, the reason and the fix, as the readiness answer does."""

    def __init__(self, gate: str, reason: str, fix: str = "", status: int = 409):
        super().__init__(reason)
        self.gate, self.reason, self.fix, self.status = gate, reason, fix, status

    def as_dict(self) -> dict:
        return {"gate": self.gate, "reason": self.reason, "fix": self.fix}


class NotFound(RuleBroken):
    """A row the caller may not see, or that is not there (404)."""


class ClaimLost(Exception):
    """Another writer took the key this one needed (a plain INSERT hit 23505). Re-read and decide again."""

    def __init__(self, table: str = "", row: dict | None = None):
        super().__init__(table)
        self.table, self.row = table, row


class ShortOf(RuleBroken):
    """A place does not hold what a move takes out of it (and negative stock is not allowed there)."""

    def __init__(self, message: str, location_id: str = "", held=None):
        super().__init__(message)
        self.location_id, self.held = location_id, held