From 825c88b6e8df51caa1ff8b51ebfd7f108e6106c8 Mon Sep 17 00:00:00 2001 From: Aristotelis Date: Wed, 24 Aug 2022 08:07:44 +0200 Subject: [PATCH] Make all Anki-native exceptions inherit from the same base class (#2028) * Make all Anki-native exceptions inherit from same base class Allows add-ons to easily catch all Anki-native exceptions without being coupled to the currently implemented exceptions. * Satisfy pylint --- pylib/anki/errors.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) 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