Add lua::humanErrorText

This commit is contained in:
Mm2PL 2023-01-31 14:58:20 +01:00
parent d830d1c960
commit 5191f4c9eb
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
3 changed files with 44 additions and 27 deletions

View file

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

View file

@ -6,12 +6,18 @@
#include <vector>
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);

View file

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