LuaBridge icon indicating copy to clipboard operation
LuaBridge copied to clipboard

Catch cast exceptions

Open Fulgen301 opened this issue 5 years ago • 3 comments

LuaExceptions thrown during a cast call can't be caught, which is a problem for some applications, e.g. casts to std::map - I can cast to std::map<LuaRef, LuaRef>, but then I have to manually check each key and value whether it is of a certain type.

Fulgen301 avatar Jun 29 '19 21:06 Fulgen301

What do you mean by "can't be caught"? There is an exception and a catch operator skips it? Could you please post the minimal code example that reproduces the issue?

dmitry-t avatar Aug 08 '19 21:08 dmitry-t

I've already figured it out for myself, forgot to update this issue, sorry :/ The problem is that failed casts generate a Lua error, which isn't re-raised as LuaException, but can only be caught by lua_pcall. The solution I used was to wrap the cast in a call to lua_pcall and return an empty std::optional on a failed cast.

Fulgen301 avatar Aug 09 '19 18:08 Fulgen301

Actually if you had at least one luagridge::getGlobalNamespace (L) call, Lua errors would be translated to C++ exceptions. Such behavior was introduced in the version 2.3.1.

Also I'm going to provide an explicit luabridge::enableExceptions (L) function (which will still be implicitly invoked by luagridge::getGlobalNamespace (L)). The implementation is pretty simple: set atpanic handler (via lua_atpanic) which will get the error message from the stack and throw an exception with the what message.

So my questions: what LuaBridge version did you use and did you do any class/function/data registration?

P.S. My personal opinion about heterogeneous Lua tables is that they should be cast to std::map <LuaRef, LuaRef> - exactly as you do (though it seems to be a rather special use case).

dmitry-t avatar Oct 13 '19 17:10 dmitry-t