From 7b665848589fcbd88d818d24877627516a645894 Mon Sep 17 00:00:00 2001 From: Amadej Kastelic Date: Thu, 17 Oct 2024 17:40:29 +0200 Subject: [PATCH] Retry on SSL errors (#21) --- bot/adapters/discord/client.py | 6 +++++- bot/common/utils.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bot/adapters/discord/client.py b/bot/adapters/discord/client.py index fa5612f..46bc911 100644 --- a/bot/adapters/discord/client.py +++ b/bot/adapters/discord/client.py @@ -383,12 +383,16 @@ async def _send_post( if e.status != 413: # Payload too large raise e if post.buffer is not None: - logger.info('File too large, resizing...') + logger.info('File too large, resizing...', size=len(post.buffer)) post.buffer.seek(0) post.buffer = await utils.resize(buffer=post.buffer, extension=extension) return await self._send_post(post=post, send_func=send_func, author=author) raise exceptions.BotError('Failed to send message') from e + except utils.SSL_ERRORS as e: + # Retry on SSL errors + logger.error("SSL Error, retrying", error=str(e)) + return await self._send_post(post=post, send_func=send_func, author=author) async def _send_comments( self, diff --git a/bot/common/utils.py b/bot/common/utils.py index 09a3bf5..4d17469 100644 --- a/bot/common/utils.py +++ b/bot/common/utils.py @@ -9,12 +9,20 @@ import typing from contextlib import contextmanager +import aiohttp import magic from PIL import Image as pil_image from django import db as django_db +from requests import exceptions as requests_exceptions emoji = ['😼', '😺', '😸', '😹', '😻', '🙀', '😿', '😾', '😩', '🙈', '🙉', '🙊', '😳'] +SSL_ERRORS = ( + requests_exceptions.SSLError, + aiohttp.ClientSSLError, + aiohttp.ClientConnectorSSLError, +) + def find_first_url(string: str) -> typing.Optional[str]: urls = re.findall(r'(https?://[^\s]+)', string)