[FIX] LilyPond mode no longer crashes on bad input

This commit is contained in:
Load Accumulator 2023-06-14 19:36:13 +02:00
parent 010bd2634b
commit 3ebc41fddb
No known key found for this signature in database
GPG key ID: 6898757653ABE3E6
2 changed files with 15 additions and 4 deletions

View file

@ -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")

View file

@ -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.<br><code><pre>{e.args[0]}</pre></code>",
}
await client.room_send(room, message_type="m.room.message", content=content)