From 8182bb1a01e2f9eba1fbb79b9909ac02d5860b08 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Tue, 12 May 2026 01:02:57 +0800 Subject: [PATCH] feat(lib): add errors module Introduce AiMouseError base class plus ModelLoadError and GenerationError subclasses so downstream consumers can catch the umbrella or specific failure modes. Co-Authored-By: Claude Opus 4.7 --- src/ai_mouse/errors.py | 18 ++++++++++++++++++ tests/unit/test_errors.py | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/ai_mouse/errors.py create mode 100644 tests/unit/test_errors.py diff --git a/src/ai_mouse/errors.py b/src/ai_mouse/errors.py new file mode 100644 index 0000000..d5cd1fd --- /dev/null +++ b/src/ai_mouse/errors.py @@ -0,0 +1,18 @@ +"""Exception hierarchy for the ai_mouse library. + +Downstream consumers can catch the umbrella :class:`AiMouseError` +or the specific subclasses for finer control. +""" +from __future__ import annotations + + +class AiMouseError(Exception): + """Base class for all ai_mouse errors.""" + + +class ModelLoadError(AiMouseError): + """Raised when ONNX weights / metadata cannot be loaded.""" + + +class GenerationError(AiMouseError): + """Raised when inference produces an invalid result (e.g. NaN).""" diff --git a/tests/unit/test_errors.py b/tests/unit/test_errors.py new file mode 100644 index 0000000..9f9cc10 --- /dev/null +++ b/tests/unit/test_errors.py @@ -0,0 +1,19 @@ +"""Test the error hierarchy.""" +from __future__ import annotations + +import pytest + +from ai_mouse import errors + + +def test_model_load_error_is_aimouse_error() -> None: + assert issubclass(errors.ModelLoadError, errors.AiMouseError) + + +def test_generation_error_is_aimouse_error() -> None: + assert issubclass(errors.GenerationError, errors.AiMouseError) + + +def test_can_catch_specific_with_general() -> None: + with pytest.raises(errors.AiMouseError): + raise errors.ModelLoadError("test")