diff options
author | 2023-06-06 07:54:21 +0100 | |
---|---|---|
committer | 2023-06-06 06:54:21 +0000 | |
commit | de2eab48d2514695ffee3c4f99c4f8ebf36c7cbb (patch) | |
tree | 446657f5b67afef5be77318c7a6db59d5ae41a85 /bot/exts/fun/latex.py | |
parent | Bump sentry-sdk from 1.24.0 to 1.25.0 (#1288) (diff) |
Add support for latex rendering server >400 errors (#1278)
Diffstat (limited to 'bot/exts/fun/latex.py')
-rw-r--r-- | bot/exts/fun/latex.py | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/bot/exts/fun/latex.py b/bot/exts/fun/latex.py index 12f2d0b6..b7bd708e 100644 --- a/bot/exts/fun/latex.py +++ b/bot/exts/fun/latex.py @@ -9,7 +9,7 @@ from typing import BinaryIO import discord from PIL import Image -from aiohttp import web +from aiohttp import client_exceptions, web from discord.ext import commands from bot.bot import Bot @@ -72,6 +72,11 @@ class InvalidLatexError(Exception): self.logs = logs +class LatexServerError(Exception): + """Represents an error raised from Latex rendering server.""" + + + class Latex(commands.Cog): """Renders latex.""" @@ -81,8 +86,11 @@ class Latex(commands.Cog): async def _generate_image(self, query: str, out_file: BinaryIO) -> None: """Make an API request and save the generated image to cache.""" payload = {"code": query, "format": "png"} - async with self.bot.http_session.post(LATEX_API_URL, data=payload, raise_for_status=True) as response: - response_json = await response.json() + try: + async with self.bot.http_session.post(LATEX_API_URL, data=payload, raise_for_status=True) as response: + response_json = await response.json() + except client_exceptions.ClientResponseError: + raise LatexServerError if response_json["status"] != "success": raise InvalidLatexError(logs=response_json.get("log")) async with self.bot.http_session.get( @@ -105,6 +113,21 @@ class Latex(commands.Cog): except web.HTTPClientError as e: log.info("Error when uploading latex output to pastebin. %s", e) + async def _prepare_error_embed(self, err: InvalidLatexError | LatexServerError | None) -> discord.Embed: + title = "Server encountered an issue, please retry later." + if isinstance(err, InvalidLatexError): + title = "Failed to render input." + + embed = discord.Embed(title=title) + embed.description = "No logs available." + logs = getattr(err, "logs", None) + if logs: + logs_paste_url = await self._upload_to_pastebin(logs) + embed.description = "Couldn't upload logs." + if logs_paste_url: + embed.description = f"[View Logs]({logs_paste_url})" + return embed + @commands.command() @commands.max_concurrency(1, commands.BucketType.guild, wait=True) @whitelist_override(channels=LATEX_ALLOWED_CHANNNELS) @@ -120,16 +143,8 @@ class Latex(commands.Cog): try: with open(image_path, "wb") as out_file: await self._generate_image(TEMPLATE.substitute(text=query), out_file) - except InvalidLatexError as err: - embed = discord.Embed(title="Failed to render input.") - if err.logs is None: - embed.description = "No logs available." - else: - logs_paste_url = await self._upload_to_pastebin(err.logs) - if logs_paste_url: - embed.description = f"[View Logs]({logs_paste_url})" - else: - embed.description = "Couldn't upload logs." + except (InvalidLatexError, LatexServerError) as err: + embed = await self._prepare_error_embed(err) await ctx.send(embed=embed) image_path.unlink() return |