Elmi
Elmi

Reputation: 6193

Catch output of lua_error()?

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?

Upvotes: 3

Views: 1941

Answers (2)

ComicSansMS
ComicSansMS

Reputation: 54589

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.

Upvotes: 3

lhf
lhf

Reputation: 72312

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.

Upvotes: 0

Related Questions