3

Currently I'm using lua out of an console application, means whenever there is an error in script and lua_error() is called, the related text it is printed.

Now I have to move that thingy into a GUI-application that runs without a console in background. Error text that appear have to be submitted to the main application so that it is able to do whatever is necessary with it. As a second point the whole application has NOT to be stopped (like it happened for the console-application).

So my question: Is there a possibility to catch the error messages given with lua_error() at some point in code for further processing and to avoid the application is stopped?

2 Answers 2

3

As the Lua manual points out, you will want to use protected calls for executing the Lua code from your application. If you encounter an error from an unprotected call, Lua will call a panic function, which writes the error output to the console that you wanted to get rid of.

In particular, lua_pcall may be provided with a custom message handler that is invoked in case of an error. You could provide a custom handler that interacts with your GUI.

Be aware though that message handlers are invoked via a C-longjmp, which has certain implications on what you can and can not do from within a message handler.

Alternatively, don't provide a message handler at all and instead check the return value of the lua_pcall function. In case of an error, the error message can be retrieved from the top of the Lua stack.

Sign up to request clarification or add additional context in comments.

1 Comment

Another (and I think simpler) way is to check return value of lua_pcall. If it's different that 0, the error message is right there on the stack, you just need to get it with lua_to(l)string.
0

lua_error by itself does not emit any output: it's the application that called Lua that handles how to report error.

By console, I assume you mean you're using the standard Lua command-line interpreter. It seems that you now want to embed Lua into your application. In this case, you use lua_pcall to run Lua scripts and check its return value, handling any errors that may happen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.