diff --git a/pylib/anki/errors.py b/pylib/anki/errors.py index 35fb6d013..b1ca0c7c6 100644 --- a/pylib/anki/errors.py +++ b/pylib/anki/errors.py @@ -10,7 +10,22 @@ if TYPE_CHECKING: import anki.collection -class LocalizedError(Exception): +class AnkiException(Exception): + """ + General Anki exception that all custom exceptions raised by Anki should + inherit from. Allows add-ons to easily identify Anki-native exceptions. + + When inheriting from a Python built-in exception other than `Exception`, + please supply `AnkiException` as an additional inheritance: + + ``` + class MyNewAnkiException(ValueError, AnkiException): + pass + ``` + """ + + +class LocalizedError(AnkiException): "An error with a localized description." def __init__(self, localized: str) -> None: @@ -29,7 +44,7 @@ class DocumentedError(LocalizedError): super().__init__(localized) -class Interrupted(Exception): +class Interrupted(AnkiException): pass @@ -68,7 +83,7 @@ class TemplateError(LocalizedError): pass -class NotFoundError(Exception): +class NotFoundError(AnkiException): pass @@ -76,11 +91,11 @@ class DeletedError(LocalizedError): pass -class ExistsError(Exception): +class ExistsError(AnkiException): pass -class UndoEmpty(Exception): +class UndoEmpty(AnkiException): pass @@ -96,7 +111,7 @@ class SearchError(LocalizedError): pass -class AbortSchemaModification(Exception): +class AbortSchemaModification(AnkiException): pass