From 85c1c9a3b1838eef510a10054be1c70ab5f61681 Mon Sep 17 00:00:00 2001 From: Carl Oscar Aaro Date: Fri, 12 Jan 2018 09:47:37 +0100 Subject: [PATCH] Gracefully handle exceptions thrown when receiving messages from AWS SNS+SQS --- tomodachi/transport/aws_sns_sqs.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tomodachi/transport/aws_sns_sqs.py b/tomodachi/transport/aws_sns_sqs.py index 0ecadcbb2..262ca9a7d 100644 --- a/tomodachi/transport/aws_sns_sqs.py +++ b/tomodachi/transport/aws_sns_sqs.py @@ -10,6 +10,7 @@ import ujson import uuid import inspect +from botocore.parsers import ResponseParserError from typing import Any, Dict, Union, Optional, Callable, List, Tuple, Match try: from typing import Awaitable @@ -486,6 +487,12 @@ async def _callback() -> None: logging.getLogger('transport.aws_sns_sqs').warning('Unable to receive message from queue [sqs] on AWS ({}) - reconnecting'.format(error_message)) await asyncio.sleep(1) continue + except ResponseParserError as e: + is_disconnected = True + error_message = 'Unable to parse response: the server was not able to produce a timely response to your request' + logging.getLogger('transport.aws_sns_sqs').warning('Unable to receive message from queue [sqs] on AWS ({}) - reconnecting'.format(error_message)) + await asyncio.sleep(1) + continue except (botocore.exceptions.ClientError, aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError) as e: error_message = str(e) if not isinstance(e, asyncio.TimeoutError) else 'Network timeout' if 'AWS.SimpleQueueService.NonExistentQueue' in error_message: @@ -506,6 +513,11 @@ async def _callback() -> None: logging.getLogger('transport.aws_sns_sqs').warning('Unable to receive message from queue [sqs] on AWS ({})'.format(error_message)) await asyncio.sleep(1) continue + except Exception as e: + error_message = str(e) + logging.getLogger('transport.aws_sns_sqs').warning('Unexpected error while receiving message from queue [sqs] on AWS ({})'.format(error_message)) + await asyncio.sleep(1) + continue messages = response.get('Messages', []) if messages: