Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn AnnifException into an abstract base class and fix message prefix behavior #320

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)