forked from mirrors/bookwyrm
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 requests import HTTPError
|
||||||
|
|
||||||
from bookwyrm import book_search, models
|
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
|
from bookwyrm.tasks import app
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -33,23 +33,29 @@ async def get_results(session, url, params, query, connector):
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
async with session.get(url, headers=headers, params=params) as response:
|
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:
|
try:
|
||||||
raw_data = await response.json()
|
raw_data = await response.json()
|
||||||
except aiohttp.client_exceptions.ContentTypeError as err:
|
except aiohttp.client_exceptions.ContentTypeError as err:
|
||||||
logger.exception(err)
|
logger.exception(err)
|
||||||
return None
|
return
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"connector": connector,
|
"connector": connector,
|
||||||
"results": connector.process_search_response(query, raw_data),
|
"results": connector.process_search_response(query, raw_data),
|
||||||
}
|
}
|
||||||
except asyncio.TimeoutError:
|
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):
|
async def async_connector_search(query, items, params):
|
||||||
"""Try a number of requests simultaneously"""
|
"""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:
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
tasks = []
|
tasks = []
|
||||||
for url, connector in items:
|
for url, connector in items:
|
||||||
|
@ -73,8 +79,11 @@ def search(query, min_confidence=0.1, return_first=False):
|
||||||
for connector in get_connectors():
|
for connector in get_connectors():
|
||||||
# get the search url from the connector before sending
|
# get the search url from the connector before sending
|
||||||
url = connector.get_search_url(query)
|
url = connector.get_search_url(query)
|
||||||
# check the URL is valid
|
try:
|
||||||
raise_not_valid_url(url)
|
raise_not_valid_url(url)
|
||||||
|
except ConnectorException:
|
||||||
|
# if this URL is invalid we should skip it and move on
|
||||||
|
continue
|
||||||
items.append((url, connector))
|
items.append((url, connector))
|
||||||
|
|
||||||
# load as many results as we can
|
# load as many results as we can
|
||||||
|
@ -83,7 +92,7 @@ def search(query, min_confidence=0.1, return_first=False):
|
||||||
|
|
||||||
if return_first:
|
if return_first:
|
||||||
# find the best result from all the responses and return that
|
# 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
|
# failed requests will return None, so filter those out
|
||||||
return [r for r in results if r]
|
return [r for r in results if r]
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
{% if result_set.results %}
|
{% if result_set.results %}
|
||||||
<section class="mb-5">
|
<section class="mb-5">
|
||||||
{% if not result_set.connector.local %}
|
{% if not result_set.connector.local %}
|
||||||
<details class="details-panel box" {% if forloop.first %}open{% endif %}>
|
<details class="details-panel box" open>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if not result_set.connector.local %}
|
{% if not result_set.connector.local %}
|
||||||
<summary class="is-flex is-align-items-center is-flex-wrap-wrap is-gap-2">
|
<summary class="is-flex is-align-items-center is-flex-wrap-wrap is-gap-2">
|
||||||
|
|
Loading…
Reference in a new issue