Skip to content

Commit 1d7254d

Browse files
committed
Provide a stack trace on a lua error with flex backend
Issue #1136
1 parent 5caa410 commit 1d7254d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/output-flex.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,31 @@ void output_flex_t::add_row(table_connection_t *table_connection,
983983
}
984984
}
985985

986+
static int msghandler (lua_State *L)
987+
{
988+
const char *msg = lua_tostring(L, 1);
989+
if (msg == NULL) { /* is error object not a string? */
990+
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
991+
lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
992+
return 1; /* that is the message */
993+
else
994+
msg = lua_pushfstring(L, "(error object is a %s value)",
995+
luaL_typename(L, 1));
996+
}
997+
luaL_traceback(L, L, msg, 1); /* append a standard traceback */
998+
return 1; /* return the traceback */
999+
}
1000+
1001+
static int docall (lua_State *L, int narg, int nres) {
1002+
int status;
1003+
int base = lua_gettop(L) - narg; /* function index */
1004+
lua_pushcfunction(L, msghandler); /* push message handler */
1005+
lua_insert(L, base); /* put it under function and args */
1006+
status = lua_pcall(L, narg, nres, base);
1007+
lua_remove(L, base); /* remove message handler from the stack */
1008+
return status;
1009+
}
1010+
9861011
void output_flex_t::call_process_function(int index,
9871012
osmium::OSMObject const &object)
9881013
{
@@ -996,7 +1021,7 @@ void output_flex_t::call_process_function(int index,
9961021
get_options()->extra_attributes); // the single argument
9971022

9981023
luaX_set_context(lua_state(), this);
999-
if (lua_pcall(lua_state(), 1, 0, 0)) {
1024+
if (docall(lua_state(), 1, 0)) {
10001025
throw std::runtime_error{"Failed to execute lua processing function:"
10011026
" {}"_format(lua_tostring(lua_state(), -1))};
10021027
}

0 commit comments

Comments
 (0)