forked from mirrors/bookwyrm
Raise errors when connectors fail
This commit is contained in:
parent
56ebd6e7c0
commit
f72d59955e
4 changed files with 14 additions and 4 deletions
|
@ -1,2 +1,3 @@
|
|||
''' bring connectors into the namespace '''
|
||||
from .settings import CONNECTORS
|
||||
from .abstract_connector import ConnectorException
|
||||
|
|
|
@ -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 '''
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 '''
|
||||
|
|
Loading…
Reference in a new issue