Skip to content

Commit

Permalink
Properly generate traceback in lua_error_handler() avoiding the first…
Browse files Browse the repository at this point in the history
… line showing the manual call to debug.traceback() itself

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Sep 23, 2024
1 parent 59cfeb4 commit d6dd405
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/webserver/civetweb/mod_lua.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2798,23 +2798,34 @@ lua_error_handler(lua_State *L)
{
const char *error_msg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "?\n";

/* Log error message */
lua_cry(NULL, 0, L, error_msg, "error");

lua_getglobal(L, "mg");
if (!lua_isnil(L, -1)) {
lua_getfield(L, -1, "write"); /* call mg.write() */
/* Write the error message to the error log */
lua_getfield(L, -1, "write");
lua_pushstring(L, error_msg);
lua_pushliteral(L, "\n");
lua_call(L, 2, 0);
IGNORE_UNUSED_RESULT(
luaL_dostring(L, "mg.write(debug.traceback(), '\\n')"));
lua_call(L, 2, 0); /* call mg.write(error_msg + \n) */
lua_pop(L, 1); /* pop mg */

/* Get Lua traceback */
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_call(L, 0, 1); /* call debug.traceback() */
lua_remove(L, -2); /* remove debug */

/* Write the Lua traceback to the error log */
lua_getglobal(L, "mg");
lua_getfield(L, -1, "write");
lua_pushvalue(L, -3); /* copy the traceback */
lua_pushliteral(L, "\n"); /* append a newline */
lua_call(L, 2, 0); /* call mg.write(traceback + \n) */
lua_pop(L, 2); /* pop mg and traceback */
} else {
printf("Lua error: [%s]\n", error_msg);
IGNORE_UNUSED_RESULT(
luaL_dostring(L, "print(debug.traceback(), '\\n')"));
}
/* TODO(lsm, low): leave the stack balanced */
lua_pop(L, 1); /* pop error message */

return 0;
}
Expand Down

0 comments on commit d6dd405

Please sign in to comment.