WeaselGames / godot_luaAPI

Godot LuaAPI
https://luaapi.weaselgames.info
Other
371 stars 28 forks source link

More properties in LuaError #101

Closed Griiimon closed 1 year ago

Griiimon commented 1 year ago

I would like to construct a custom Lua Error Message and need access to the line number for example. As far as I can tell you aren't constructing the LuaError string yourself but rather pass along what you are getting from lua?

If so, I guess there is really nothing to be done but to parse the String. Just wanted to make sure I'm not missing something here, still having trouble figuring out how this extension works exactly.

Trey2k commented 1 year ago

So as far as how we handle error handling. We have this method here which takes the error from the lua stack and create the LuaError: https://github.com/WeaselGames/godot_luaAPI/blob/73a6eb9ea0b87a0bea5137cf1f3f48caaa353c15/src/luaState.cpp#L502-L535

To append the stack trace we have this method which we pass as our lua error handler for protected calls: https://github.com/WeaselGames/godot_luaAPI/blob/73a6eb9ea0b87a0bea5137cf1f3f48caaa353c15/src/luaState.cpp#L636-L641

You can see this in action in the callFunction method: https://github.com/WeaselGames/godot_luaAPI/blob/73a6eb9ea0b87a0bea5137cf1f3f48caaa353c15/src/luaState.cpp#L167-L187

Or in do file/string: https://github.com/WeaselGames/godot_luaAPI/blob/73a6eb9ea0b87a0bea5137cf1f3f48caaa353c15/src/classes/luaAPI.cpp#L69-L113

A major factor in the quality of info you get is weather you are using do file or do string. Currently the stack trace will not append as useful information in do_string. Maybe there is a way for me to fix this I am not sure. But we treat both the same as far as error handling goes.

Sample do_file error:

[LUA_ERRSYNTAX - syntax error ]
/absolute/path/test.lua:4: syntax error near 'print'

Sample do_string error:


[LUA_ERRRUN - runtime error ]
attempt to call a string value
stack traceback:

Sample code:

print("test") 
print("test") 
i
print("test") 
Trey2k commented 1 year ago

still having trouble figuring out how this extension works exactly.

If you ever have any questions I am more then happy to help. I have a discord as well.

Griiimon commented 1 year ago

Thanks, that was VERY helpful.

So i was indeed using do_string and i can easily switch to do_file.

What I'm trying to do is to remove a lot of excess information from the Error string (now including the path of the temporary lua file I create) before I pass it on to the user. In addition I need to manipulate the line number.

I'm injecting a hidden line at the start of the script which messes with the line number the user sees in his script, so i have to make sure to decrease it by the number of lines I'm adding in a potential error message. But thats something I can figure out myself by parsing the Error String which seems to have a consistent format, so i will close this Feature request.

Thanks for the discord invite!!

Trey2k commented 1 year ago

I have actually just noticed looking over this again, we actually dont error handle when the string is loaded into the state for do_string atm. do_file: https://github.com/WeaselGames/godot_luaAPI/blob/73a6eb9ea0b87a0bea5137cf1f3f48caaa353c15/src/classes/luaAPI.cpp#L92-L95 do_string: https://github.com/WeaselGames/godot_luaAPI/blob/73a6eb9ea0b87a0bea5137cf1f3f48caaa353c15/src/classes/luaAPI.cpp#L107

Luckily this should be a simple fix. Hopefully this fixes the horrible error messages we currently have.