Verified Commit a82bca0b authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Better error handling

parent 1e5eab9a
......@@ -9,6 +9,9 @@ from . import sources
from . import serializers
aiohttp_timeout = aiohttp.ClientTimeout(total=10)
async def json_response(self, status, content):
await self.send_response(
status,
......@@ -40,11 +43,14 @@ class SearchSingleConsumer(AsyncHttpConsumer):
except KeyError:
await json_response(self, 400, {"detail": "Invalid lookup"})
try:
async with aiohttp.client.ClientSession(timeout=5) as session:
async with aiohttp.client.ClientSession(timeout=aiohttp_timeout) as session:
data = await source.get(lookup, session)
profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
except (exceptions.SearchError, aiohttp.ClientError) as e:
await json_response(self, 400, {"detail": e.message})
except Exception:
await json_response(self, 500, {})
raise
await json_response(self, 200, profile)
......@@ -77,7 +83,7 @@ class SearchMultipleConsumer(AsyncHttpConsumer):
lookups = serializer.validated_data["lookups"]
results = {}
tasks = []
async with aiohttp.client.ClientSession(timeout=15) as session:
async with aiohttp.client.ClientSession(timeout=aiohttp_timeout) as session:
for lookup_type, lookup in lookups:
try:
source = sources.registry._data[lookup_type]
......@@ -94,5 +100,9 @@ class SearchMultipleConsumer(AsyncHttpConsumer):
results=results,
)
)
await asyncio.gather(*tasks)
try:
await asyncio.gather(*tasks)
except Exception as e:
await json_response(self, 500, {})
raise
await json_response(self, 200, results)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment