mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-14 12:51:08 +00:00
More error handing
Adds logging and error handling for some of the numerous ways a request could fail (the remote site is down, the url is blocked, etc). I also have the results boxes open by default, which makes it more legible imo.
This commit is contained in:
parent
45f2199c71
commit
525e2a591d
2 changed files with 17 additions and 8 deletions
|
@ -12,7 +12,7 @@ from django.db.models import signals
|
|||
from requests import HTTPError
|
||||
|
||||
from bookwyrm import book_search, models
|
||||
from bookwyrm.settings import QUERY_TIMEOUT, USER_AGENT
|
||||
from bookwyrm.settings import SEARCH_TIMEOUT, USER_AGENT
|
||||
from bookwyrm.tasks import app
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -33,23 +33,29 @@ async def get_results(session, url, params, query, connector):
|
|||
}
|
||||
try:
|
||||
async with session.get(url, headers=headers, params=params) as response:
|
||||
if not response.ok:
|
||||
logger.info("Unable to connect to %s: %s", url, response.reason)
|
||||
return
|
||||
|
||||
try:
|
||||
raw_data = await response.json()
|
||||
except aiohttp.client_exceptions.ContentTypeError as err:
|
||||
logger.exception(err)
|
||||
return None
|
||||
return
|
||||
|
||||
return {
|
||||
"connector": connector,
|
||||
"results": connector.process_search_response(query, raw_data),
|
||||
}
|
||||
except asyncio.TimeoutError:
|
||||
logger.exception("Connection timout for url: %s", url)
|
||||
logger.info("Connection timed out for url: %s", url)
|
||||
except aiohttp.ClientError as err:
|
||||
logger.exception(err)
|
||||
|
||||
|
||||
async def async_connector_search(query, items, params):
|
||||
"""Try a number of requests simultaneously"""
|
||||
timeout = aiohttp.ClientTimeout(total=QUERY_TIMEOUT)
|
||||
timeout = aiohttp.ClientTimeout(total=SEARCH_TIMEOUT)
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
tasks = []
|
||||
for url, connector in items:
|
||||
|
@ -73,8 +79,11 @@ def search(query, min_confidence=0.1, return_first=False):
|
|||
for connector in get_connectors():
|
||||
# get the search url from the connector before sending
|
||||
url = connector.get_search_url(query)
|
||||
# check the URL is valid
|
||||
raise_not_valid_url(url)
|
||||
try:
|
||||
raise_not_valid_url(url)
|
||||
except ConnectorException:
|
||||
# if this URL is invalid we should skip it and move on
|
||||
continue
|
||||
items.append((url, connector))
|
||||
|
||||
# load as many results as we can
|
||||
|
@ -83,7 +92,7 @@ def search(query, min_confidence=0.1, return_first=False):
|
|||
|
||||
if return_first:
|
||||
# find the best result from all the responses and return that
|
||||
raise Exception("Not implemented yet")
|
||||
raise Exception("Not implemented yet") # TODO
|
||||
|
||||
# failed requests will return None, so filter those out
|
||||
return [r for r in results if r]
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
{% if result_set.results %}
|
||||
<section class="mb-5">
|
||||
{% if not result_set.connector.local %}
|
||||
<details class="details-panel box" {% if forloop.first %}open{% endif %}>
|
||||
<details class="details-panel box" open>
|
||||
{% endif %}
|
||||
{% if not result_set.connector.local %}
|
||||
<summary class="is-flex is-align-items-center is-flex-wrap-wrap is-gap-2">
|
||||
|
|
Loading…
Reference in a new issue