From 714f961f5982df75804c325b5ef8eab244ef5c4c Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 30 Jan 2023 20:40:30 +0100 Subject: [PATCH] Add error handling to command execution --- src/controllers/plugins/PluginController.cpp | 33 +++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/controllers/plugins/PluginController.cpp b/src/controllers/plugins/PluginController.cpp index 3fb59711e..a613901e7 100644 --- a/src/controllers/plugins/PluginController.cpp +++ b/src/controllers/plugins/PluginController.cpp @@ -118,7 +118,38 @@ QString PluginController::tryExecPluginCommand(const QString &commandName, lua_pushstring(L, ctx.channel->getName().toStdString().c_str()); lua_setfield(L, outIdx, "channelName"); - lua_pcall(L, 1, 0, 0); + auto res = lua_pcall(L, 1, 0, 0); + if (res != LUA_OK) + { + QString errName; + switch (res) + { + case LUA_ERRRUN: + errName = "runtime error"; + break; + case LUA_ERRMEM: + errName = "memory error"; + break; + case LUA_ERRERR: + errName = "error???"; + break; + default: + errName = "unknown"; + } + const char *errText = luaL_optstring(L, -1, NULL); + if (errText != nullptr) + { + ctx.channel->addMessage( + makeSystemMessage(QString("Lua error: (%1) %2") + .arg(errName, QString(errText)))); + } + else + { + ctx.channel->addMessage( + makeSystemMessage("Lua error: " + errName)); + } + return ""; + } return ""; } }