Skip to content

Commit

Permalink
Merge pull request #320 from NatLibFi/annifexception-abstract-base-class
Browse files Browse the repository at this point in the history
Turn AnnifException into an abstract base class and fix message prefix behavior
  • Loading branch information
osma authored Aug 27, 2019
2 parents b660eed + 475bba0 commit eaf0467
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
30 changes: 14 additions & 16 deletions annif/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."""

Expand Down
22 changes: 22 additions & 0 deletions tests/test_exception.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit eaf0467

Please sign in to comment.