diff --git a/bookwyrm/books_manager.py b/bookwyrm/books_manager.py index c18e63613..bfc543dec 100644 --- a/bookwyrm/books_manager.py +++ b/bookwyrm/books_manager.py @@ -26,8 +26,10 @@ def get_or_create_book(remote_id): connector = get_or_create_connector(remote_id) + # raises ConnectorException book = connector.get_or_create_book(remote_id) - load_more_data.delay(book.id) + if book: + load_more_data.delay(book.id) return book diff --git a/bookwyrm/connectors/__init__.py b/bookwyrm/connectors/__init__.py index b525f65f5..b5d93b473 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 6d114387a..602c8d53e 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 ''' @@ -128,6 +132,9 @@ class AbstractConnector(ABC): edition.author_text = work.author_text edition.save() + if not edition: + raise ConnectorException('Unable to create book: %s' % remote_id) + return edition diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index fcfe387aa..d70ab3e23 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -6,6 +6,7 @@ from django.core.files.base import ContentFile from bookwyrm import models from .abstract_connector import AbstractConnector, SearchResult, Mapping +from .abstract_connector import ConnectorException from .abstract_connector import update_from_mappings from .abstract_connector import get_date, get_data from .openlibrary_languages import languages @@ -80,7 +81,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 +91,7 @@ class Connector(AbstractConnector): try: key = data['works'][0]['key'] except (IndexError, KeyError): - return False + raise ConnectorException('No work found for edition') url = '%s/%s' % (self.books_url, key) return get_data(url) diff --git a/bookwyrm/connectors/self_connector.py b/bookwyrm/connectors/self_connector.py index e13175973..2711bb1a2 100644 --- a/bookwyrm/connectors/self_connector.py +++ b/bookwyrm/connectors/self_connector.py @@ -50,10 +50,6 @@ class Connector(AbstractConnector): ) - def get_or_create_book(self, remote_id): - ''' this COULD be semi-implemented but I think it shouldn't be used ''' - - def is_work_data(self, data): pass diff --git a/bookwyrm/models/import_job.py b/bookwyrm/models/import_job.py index b2279bc97..9eb00e046 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 '''