Merge pull request #209 from mouse-reeve/connector-errors

Raise errors when connectors fail
This commit is contained in:
Mouse Reeve 2020-10-01 08:29:39 -07:00 committed by GitHub
commit 6b5246f06a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 8 deletions

View file

@ -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

View file

@ -1,2 +1,3 @@
''' bring connectors into the namespace '''
from .settings import CONNECTORS
from .abstract_connector import ConnectorException

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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 '''