You are probably looking for a combination of xpcall and debug.traceback. You can use xpcall to pass an error handler to it and use debug.traceback to get the stack trace:
function functionThatMayFail() error('Failed') end local success, result = xpcall(functionThatMayFail, function(err) return debug.traceback(err) end) print(success, result)
This code will print:
false xpcall.lua:2: Failed stack traceback: xpcall.lua:6: in function <xpcall.lua:6> [C]: in function 'error' xpcall.lua:2: in function <xpcall.lua:1> [C]: in function 'xpcall' xpcall.lua:5: in main chunk [C]: ?
source share