"""Exceptions raised by the formula evaluator."""
from typing import Optional


class MissingFieldError(Exception):
    """Raised when a formula references a field whose value is None/missing on the record.

    Callers should surface a 422 with the included ``field_id`` so the FE
    can show "this required field needs a value" without the user having
    to inspect the formula expression.
    """

    def __init__(self, field_id: str, message: Optional[str] = None):
        self.field_id = field_id
        self.message = message or f"Field '{field_id}' is empty or missing on the record"
        super().__init__(self.message)


class FormulaEvaluationError(Exception):
    """Raised when a formula fails to evaluate for non-missing-field reasons.

    Examples: malformed expression, division by zero, unsupported token
    shape, simpleeval rejection. Callers may surface a 422 or 400.
    """

    def __init__(self, message: str, expression: Optional[str] = None):
        self.expression = expression
        super().__init__(message)
