🟡 pydantic

  • 🙁 Pydantic models will raise an error if to try to inherit them from Exception and then raise,
  • 🙂 but we can use Pydantic dataclasses instead.

examples/pydantic_exception.py

from pydantic.dataclasses import dataclass as pydantic_dataclass

from documented import DocumentedError


@pydantic_dataclass
class PydanticError(DocumentedError):
    """
    Incorrect answer!

    (To the Question of Life, Universe, and Everything.)

        - Answer given: {self.answer}
        - Correct answer: indubitably 42.
    """

    answer: str


raise PydanticError(answer='bebebe')

python

Traceback (most recent call last):
  File "📂/pydantic_exception.py", line 20, in <module>
    raise PydanticError(answer='bebebe')
PydanticError: Incorrect answer!

(To the Question of Life, Universe, and Everything.)

    - Answer given: bebebe
    - Correct answer: indubitably 42.