From f72d59955edab238dcd96284094bf8058de06d12 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 30 Sep 2020 10:27:40 -0700 Subject: [PATCH 1/3] Raise errors when connectors fail --- bookwyrm/connectors/__init__.py | 1 + bookwyrm/connectors/abstract_connector.py | 4 ++++ bookwyrm/connectors/openlibrary.py | 6 +++--- bookwyrm/models/import_job.py | 7 ++++++- 4 files changed, 14 insertions(+), 4 deletions(-) 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 e9c25f8e0..387b994fa 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 d74735d20..ce1ca7473 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 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 ''' From fe83f5d44299b05a55f9876815b169e268b1be41 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 30 Sep 2020 19:57:25 -0700 Subject: [PATCH 2/3] small style fixes and typo --- bookwyrm/connectors/openlibrary.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index ce1ca7473..e62ce93c1 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -5,7 +5,8 @@ import requests from django.core.files.base import ContentFile from bookwyrm import models -from .abstract_connector import AbstractConnector, ConnectorException, SearchResult, Mapping +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 @@ -90,7 +91,7 @@ class Connector(AbstractConnector): try: key = data['works'][0]['key'] except (IndexError, KeyError): - raise ConnectorException('No work found foredition') + raise ConnectorException('No work found for edition') url = '%s/%s' % (self.books_url, key) return get_data(url) From 94d5986ff21166aed5df3d26a1086af1125b7b56 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 30 Sep 2020 20:09:25 -0700 Subject: [PATCH 3/3] More error handling in connector/books manager --- bookwyrm/books_manager.py | 4 +++- bookwyrm/connectors/abstract_connector.py | 3 +++ bookwyrm/connectors/self_connector.py | 4 ---- 3 files changed, 6 insertions(+), 5 deletions(-) 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/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 387b994fa..9d687a80e 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -132,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/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