Skip to content
Snippets Groups Projects
Verified Commit a82bca0b authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Better error handling

parent 1e5eab9a
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,9 @@ from . import sources ...@@ -9,6 +9,9 @@ from . import sources
from . import serializers from . import serializers
aiohttp_timeout = aiohttp.ClientTimeout(total=10)
async def json_response(self, status, content): async def json_response(self, status, content):
await self.send_response( await self.send_response(
status, status,
...@@ -40,11 +43,14 @@ class SearchSingleConsumer(AsyncHttpConsumer): ...@@ -40,11 +43,14 @@ class SearchSingleConsumer(AsyncHttpConsumer):
except KeyError: except KeyError:
await json_response(self, 400, {"detail": "Invalid lookup"}) await json_response(self, 400, {"detail": "Invalid lookup"})
try: 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) data = await source.get(lookup, session)
profile = sources.result_to_retribute_profile(lookup_type, lookup, data) profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
except (exceptions.SearchError, aiohttp.ClientError) as e: except (exceptions.SearchError, aiohttp.ClientError) as e:
await json_response(self, 400, {"detail": e.message}) await json_response(self, 400, {"detail": e.message})
except Exception:
await json_response(self, 500, {})
raise
await json_response(self, 200, profile) await json_response(self, 200, profile)
...@@ -77,7 +83,7 @@ class SearchMultipleConsumer(AsyncHttpConsumer): ...@@ -77,7 +83,7 @@ class SearchMultipleConsumer(AsyncHttpConsumer):
lookups = serializer.validated_data["lookups"] lookups = serializer.validated_data["lookups"]
results = {} results = {}
tasks = [] 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: for lookup_type, lookup in lookups:
try: try:
source = sources.registry._data[lookup_type] source = sources.registry._data[lookup_type]
...@@ -94,5 +100,9 @@ class SearchMultipleConsumer(AsyncHttpConsumer): ...@@ -94,5 +100,9 @@ class SearchMultipleConsumer(AsyncHttpConsumer):
results=results, results=results,
) )
) )
try:
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
except Exception as e:
await json_response(self, 500, {})
raise
await json_response(self, 200, results) await json_response(self, 200, results)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment