Skip to content

Commit

Permalink
ruleset: improve error formatting
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Nov 10, 2024
1 parent 1f58d51 commit e67ace0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/ruleset.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
static int l_panic(lua_State *L)
{
const int type = lua_type(L, -1);
if (type == LUA_TSTRING) {
switch (type) {
case LUA_TNIL:
LOG_STACK(FATAL, 0, "panic: (nil)");
break;
case LUA_TSTRING:
LOG_STACK_F(FATAL, 0, "panic: %s", lua_tostring(L, -1));
} else {
break;
default:
LOG_STACK_F(
FATAL, 0, "panic: (%s: %p)", lua_typename(L, type),
lua_topointer(L, -1));
Expand Down
25 changes: 17 additions & 8 deletions src/ruleset/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,26 @@ struct dialreq *aux_todialreq(lua_State *restrict L, const int n)

int aux_traceback(lua_State *restrict L)
{
const char *err = lua_tostring(L, 1);
const int type = lua_type(L, 1);
const char *err;
switch (type) {
case LUA_TNIL:
err = "(nil)";
break;
case LUA_TNUMBER:
case LUA_TSTRING:
err = lua_tostring(L, 1);
break;
default:
err = lua_pushfstring(
L, "(%s: %p)", lua_typename(L, type),
lua_topointer(L, 1));
}
LOG_STACK_F(WARNING, 0, "traceback: %s", err);
luaL_traceback(L, L, err, 1);
size_t len;
const char *s = lua_tolstring(L, -1, &len);
if (err == NULL) {
LOG_STACK(WARNING, 0, "C traceback");
LOG_TXT(WARNING, s, len, "Lua traceback");
return 1;
}
LOG_STACK_F(WARNING, 0, "C traceback for `%s'", err);
LOG_TXT_F(WARNING, s, len, "Lua traceback for `%s'", err);
LOG_TXT_F(VERBOSE, s, len, "traceback: %s", err);
return 1;
}

Expand Down

0 comments on commit e67ace0

Please sign in to comment.