diff --git a/annif/exception.py b/annif/exception.py index 867eb2beb..9acbd052b 100644 --- a/annif/exception.py +++ b/annif/exception.py @@ -6,29 +6,19 @@ class AnnifException(ClickException): """Base Annif exception. We define this as a subclass of ClickException so - that the CLI can automatically handle exceptions.""" + that the CLI can automatically handle exceptions. This exception cannot be + instantiated directly - subclasses should be used instead.""" def __init__(self, message, project_id=None, backend_id=None): super().__init__(message) self.project_id = project_id self.backend_id = backend_id - def format_message(self): - if self.project_id is not None: - return "Project '{}': {}".format(self.project_id, - self.message) - if self.backend_id is not None: - return "Backend '{}': {}".format(self.backend_id, - self.message) - return "Error: {}".format(self.message) - - -class NotInitializedException(AnnifException): - """Exception raised for attempting to use a project or backend that - cannot be initialized, most likely since it is not yet functional - because of lack of vocabulary or training.""" + if self.prefix is None: + raise TypeError("Cannot instantiate exception without a prefix.") - prefix = "Couldn't initialize" + # subclasses should set this to a descriptive prefix + prefix = None def format_message(self): if self.project_id is not None: @@ -42,6 +32,14 @@ def format_message(self): return "{}: {}".format(self.prefix, self.message) +class NotInitializedException(AnnifException): + """Exception raised for attempting to use a project or backend that + cannot be initialized, most likely since it is not yet functional + because of lack of vocabulary or training.""" + + prefix = "Couldn't initialize" + + class ConfigurationException(AnnifException): """Exception raised when a project or backend is misconfigured.""" diff --git a/tests/test_exception.py b/tests/test_exception.py new file mode 100644 index 000000000..3fda48b5b --- /dev/null +++ b/tests/test_exception.py @@ -0,0 +1,22 @@ +"""Unit tests for Annif exception classes""" + +import pytest +from annif.exception import AnnifException +from click import ClickException + + +def test_annifexception_not_instantiable(): + with pytest.raises(TypeError): + exc = AnnifException("test message") + + +def test_annifexception_is_clickexception(): + + # we need to define a custom class to make an instantiable exception + class CustomException(AnnifException): + @property + def prefix(self): + return "my prefix" + + exc = CustomException("test message") + assert isinstance(exc, ClickException)