sol2
sol2 copied to clipboard
Catching error
How can I catch the error when trying to call a non-existent user type function, because when I try to call a non-existent function, it crashes in stack::remove in the lua_gettop call
My code cpp:
this->m_state.new_usertype <color_picker>("color_picker", sol::base_classes, sol::bases<gui_element_new>(),
"get_value", &color_picker::get_value,
"set_value", &color_picker::set_value
);
lua:
color_picker:get_value1() -- crash
Are you compiling with SOL_ALL_SAFETIES_ON
(https://sol2.readthedocs.io/en/latest/safety.html)? Are you running the code in a protected_function
and catching the errors (https://sol2.readthedocs.io/en/latest/api/state.html?highlight=safe_script#state-script-function)?
Yes, I compile with SOL_ALL_SAFETIES_ON and use protected_function
#define SOL_ALL_SAFETIES_ON 1
#define SOL_CHECK_ARGUMENTS
#define SOL_EXCEPTIONS_SAFE_PROPAGATION 1
#include "sol.hpp"
auto callbacks = state->get_callbacks(crypt_str("on_paint"));
for (auto& callback : callbacks)
{
sol::protected_function_result result = callback.get_function()();
if (!result.valid()) {
this->m_current_event = CALLBACK_NONE;
sol::error error = result;
auto log = crypt_str("Lua error: ") + (std::string)error.what() + " " + state->get_name();
logs->add(log, Color::Red, crypt_str("[ ") + state->get_name() + crypt_str(" ] "));
this->unload(state->get_name());
}
}
get_function() return sol::protected_function