diff --git a/bookwyrm/connectors/__init__.py b/bookwyrm/connectors/__init__.py index b525f65f..b5d93b47 100644 --- a/bookwyrm/connectors/__init__.py +++ b/bookwyrm/connectors/__init__.py @@ -1,2 +1,3 @@ ''' bring connectors into the namespace ''' from .settings import CONNECTORS +from .abstract_connector import ConnectorException diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index e9c25f8e..387b994f 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -9,6 +9,10 @@ from django.db import transaction from bookwyrm import models +class ConnectorException(Exception): + ''' when the connector can't do what was asked ''' + + class AbstractConnector(ABC): ''' generic book data connector ''' diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index d74735d2..ce1ca747 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -5,7 +5,7 @@ import requests from django.core.files.base import ContentFile from bookwyrm import models -from .abstract_connector import AbstractConnector, SearchResult, Mapping +from .abstract_connector import AbstractConnector, ConnectorException, SearchResult, Mapping from .abstract_connector import update_from_mappings from .abstract_connector import get_date, get_data from .openlibrary_languages import languages @@ -80,7 +80,7 @@ class Connector(AbstractConnector): try: key = data['key'] except KeyError: - return False + raise ConnectorException('Invalid book data') url = '%s/%s/editions' % (self.books_url, key) data = get_data(url) return pick_default_edition(data['entries']) @@ -90,7 +90,7 @@ class Connector(AbstractConnector): try: key = data['works'][0]['key'] except (IndexError, KeyError): - return False + raise ConnectorException('No work found foredition') url = '%s/%s' % (self.books_url, key) return get_data(url) diff --git a/bookwyrm/models/import_job.py b/bookwyrm/models/import_job.py index b2279bc9..9eb00e04 100644 --- a/bookwyrm/models/import_job.py +++ b/bookwyrm/models/import_job.py @@ -6,6 +6,7 @@ from django.db import models from django.utils import timezone from bookwyrm import books_manager +from bookwyrm.connectors import ConnectorException from bookwyrm.models import ReadThrough, User, Book from bookwyrm.utils.fields import JSONField @@ -65,7 +66,11 @@ class ImportItem(models.Model): ''' search by isbn ''' search_result = books_manager.first_search_result(self.isbn) if search_result: - return books_manager.get_or_create_book(search_result.key) + try: + # don't crash the import when the connector fails + return books_manager.get_or_create_book(search_result.key) + except ConnectorException: + pass def get_book_from_title_author(self): ''' search by title and author '''