From 5191f4c9eba2dd1413c03cb1f882ad1159a6d1c7 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Tue, 31 Jan 2023 14:58:20 +0100 Subject: [PATCH] Add lua::humanErrorText --- src/controllers/plugins/LuaUtilities.cpp | 36 ++++++++++++++++++++ src/controllers/plugins/LuaUtilities.hpp | 6 ++++ src/controllers/plugins/PluginController.cpp | 29 ++-------------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/controllers/plugins/LuaUtilities.cpp b/src/controllers/plugins/LuaUtilities.cpp index 1f493b95d..99977a79b 100644 --- a/src/controllers/plugins/LuaUtilities.cpp +++ b/src/controllers/plugins/LuaUtilities.cpp @@ -10,6 +10,42 @@ namespace chatterino::lua { +QString humanErrorText(lua_State *L, int errCode) +{ + QString errName; + switch (errCode) + { + case LUA_OK: + return "ok"; + case LUA_ERRRUN: + errName = "(runtime error)"; + break; + case LUA_ERRMEM: + errName = "(memory error)"; + break; + case LUA_ERRERR: + errName = "(error while handling another error)"; + break; + case LUA_ERRSYNTAX: + errName = "(syntax error)"; + break; + case LUA_YIELD: + errName = "(illegal coroutine yield)"; + break; + case LUA_ERRFILE: + errName = "(file error)"; + break; + default: + errName = "(unknown error type)"; + } + QString errText; + if (peek(L, &errText)) + { + errName += " " + errText; + } + return errName; +} + StackIdx pushEmptyArray(lua_State *L, int countArray) { lua_createtable(L, countArray, 0); diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp index 841b9f9dc..0ecf25c36 100644 --- a/src/controllers/plugins/LuaUtilities.hpp +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -6,12 +6,18 @@ #include struct lua_State; +class QJsonObject; namespace chatterino { struct CommandContext; } // namespace chatterino namespace chatterino::lua { +/** + * @brief Converts a lua error code and potentially string on top of the stack into a human readable message + */ +QString humanErrorText(lua_State *L, int errCode); + using StackIdx = int; StackIdx pushEmptyArray(lua_State *L, int countArray); diff --git a/src/controllers/plugins/PluginController.cpp b/src/controllers/plugins/PluginController.cpp index 7a47b5087..a0075d554 100644 --- a/src/controllers/plugins/PluginController.cpp +++ b/src/controllers/plugins/PluginController.cpp @@ -145,33 +145,8 @@ QString PluginController::tryExecPluginCommand(const QString &commandName, 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)); - } + ctx.channel->addMessage(makeSystemMessage( + "Lua error: " + lua::humanErrorText(L, res))); return ""; } return "";