Get Lua runtime errors inside script

I do not know how to efficiently debug scripts. I need stack output such as Python, but by default Lua / C does not have this. I do not know how to do that. Or just how to get the error output from the script?

+4
source share
2 answers

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]: ? 
+6
source

The Lua interpreter defaults to error. For instance. (I entered a typo in this script):

 $ lua random.lua lua: random.lua:6: attempt to call global 'xists' (a nil value) stack traceback: random.lua:6: in main chunk [C]: ? 

Can you clarify what you are trying to do, or maybe better, what is not expected, what do you expect, in terms of error output?

0
source

Source: https://habr.com/ru/post/1434469/


All Articles