[ADD] Show error logs when something bad occurs.

This commit is contained in:
LoaD Accumulator 2023-06-12 19:46:59 +02:00
parent bf6f537d21
commit 2041487fa0
No known key found for this signature in database
GPG key ID: 6898757653ABE3E6
2 changed files with 14 additions and 7 deletions

View file

@ -24,9 +24,9 @@ def render(user: str, source: str, png: bool = True) -> str:
tmp.close() tmp.close()
# Parse our templated file thru' LaTeX and dvipng. # Parse our templated file thru' LaTeX and dvipng.
# TODO: Allow arbitrary DPI and foreground color. # TODO: Allow arbitrary DPI and foreground color.
ret = subprocess.run(["latex", "-halt-on-error", tmp.name], cwd="/tmp").returncode ret = subprocess.run(["latex", "-halt-on-error", tmp.name], cwd="/tmp", capture_output=True)
if ret != 0: if ret.returncode != 0:
raise FileNotFoundError("DVI couldn't be built.") raise FileNotFoundError(ret.stdout.decode())
if png: if png:
print("PNG") print("PNG")

15
main.py
View file

@ -42,16 +42,23 @@ async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
try: try:
filename = latex.render("", event.body) filename = latex.render("", event.body)
await send_png(room, filename) await send_png(room, filename)
except (FileNotFoundError, OSError): except FileNotFoundError as e:
content = { content = {
"msgtype": "m.text", "msgtype": "m.text",
"body": "Couldn't parse LaTeX correctly." "body": f"Couldn't parse LaTeX correctly.\n```{e.args[0]}\n```",
"formatted_body": f"Couldn't parse LaTeX correctly.<br><code><pre>{e.args[0]}</pre></code>",
} }
await client.room_send(room.room_id, message_type="m.room.message", content=content) await client.room_send(room.room_id, message_type="m.room.message", content=content)
except OSError:
content = {
"msgtype": "m.text",
"body": "???",
"formatted_body": "???"
}
await client.room_send(room.room_id, message_type="m.room.message", content=content)
# Our file callback. # Our file callback.
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None: async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
url = event.url url = event.url
filename = event.body filename = event.body