From 3ebc41fddb763301f0bf4a2760f0c29761c4c37c Mon Sep 17 00:00:00 2001 From: Load Accumulator Date: Wed, 14 Jun 2023 19:36:13 +0200 Subject: [PATCH] [FIX] LilyPond mode no longer crashes on bad input --- lilypond.py | 5 ++++- routes.py | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lilypond.py b/lilypond.py index 7680d18..b1652d0 100644 --- a/lilypond.py +++ b/lilypond.py @@ -17,7 +17,10 @@ def render(user: str, source: str, png: bool = True) -> str: tmp.write(bytes(fmt, encoding="utf8")) tmp.close() if png: - subprocess.run(["lilypond", "-d", "preview", "--png", "-dresolution=500", tmp.name], cwd="/tmp") + ret = subprocess.run(["lilypond", "-d", "preview", "--png", "-dresolution=500", tmp.name], cwd="/tmp", capture_output=True) + if ret.returncode != 0: + raise FileNotFoundError(ret.stderr.decode()) + return tmp.name + ".preview.png" subprocess.run(["lilypond", "-d", "preview", "--svg", tmp.name], cwd="/tmp") diff --git a/routes.py b/routes.py index 889fb27..02594c0 100644 --- a/routes.py +++ b/routes.py @@ -16,7 +16,7 @@ async def route_help(r: Router, client: Client, room: str, event: RoomMessageTex async def route_delete(router: Router, client: Client, room: str, event: RoomMessageText, args: list) -> None: - if len(args) == 1: + if len(args) == 1: import utils import os @@ -35,5 +35,13 @@ async def route_lshow(router: Router, client: Client, room: str, event: RoomMess import lilypond import utils if len(args) == 1: - file = lilypond.render("", args[0]) - await utils.send_png(room, file) + try: + file = lilypond.render("", args[0]) + await utils.send_png(room, file) + except FileNotFoundError as e: + content = { + "msgtype": "m.text", + "body": f"Couldn't parse LilyPond correctly.\n```{e.args[0]}\n```", + "formatted_body": f"Couldn't parse LilyPond correctly.
{e.args[0]}
", + } + await client.room_send(room, message_type="m.room.message", content=content)