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
This commit is contained in:
Aristotelis 2022-08-24 08:07:44 +02:00 committed by GitHub
parent 79fbb6c8d8
commit 825c88b6e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,7 +10,22 @@ if TYPE_CHECKING:
import anki.collection 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." "An error with a localized description."
def __init__(self, localized: str) -> None: def __init__(self, localized: str) -> None:
@ -29,7 +44,7 @@ class DocumentedError(LocalizedError):
super().__init__(localized) super().__init__(localized)
class Interrupted(Exception): class Interrupted(AnkiException):
pass pass
@ -68,7 +83,7 @@ class TemplateError(LocalizedError):
pass pass
class NotFoundError(Exception): class NotFoundError(AnkiException):
pass pass
@ -76,11 +91,11 @@ class DeletedError(LocalizedError):
pass pass
class ExistsError(Exception): class ExistsError(AnkiException):
pass pass
class UndoEmpty(Exception): class UndoEmpty(AnkiException):
pass pass
@ -96,7 +111,7 @@ class SearchError(LocalizedError):
pass pass
class AbortSchemaModification(Exception): class AbortSchemaModification(AnkiException):
pass pass