2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2017-08-25 02:39:02 +08:00
|
|
|
|
2017-12-26 12:27:22 +08:00
|
|
|
#include "assert.hpp"
|
2017-04-03 14:45:06 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
int main(int, char**) {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== script error handling ===" << std::endl;
|
2017-04-03 14:45:06 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
std::string code = R"(
|
|
|
|
bad&$#*$syntax
|
|
|
|
bad.code = 2
|
|
|
|
return 24
|
|
|
|
)";
|
|
|
|
|
|
|
|
/* OPTION 1 */
|
|
|
|
// Handling code like this can be robust
|
|
|
|
// If you disable exceptions, then obviously you would remove
|
|
|
|
// the try-catch branches,
|
2020-11-20 05:42:02 +08:00
|
|
|
// and then rely on the `lua_atpanic` function being called
|
2017-04-03 14:45:06 +08:00
|
|
|
// and trapping errors there before exiting the application
|
|
|
|
{
|
2017-08-07 14:07:21 +08:00
|
|
|
// script_default_on_error throws / panics when the code is bad: trap the error
|
2017-04-03 14:45:06 +08:00
|
|
|
try {
|
2017-08-07 14:07:21 +08:00
|
|
|
int value = lua.script(code, sol::script_default_on_error);
|
2017-04-03 14:45:06 +08:00
|
|
|
// This will never be reached
|
2017-04-03 20:19:16 +08:00
|
|
|
std::cout << value << std::endl;
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(value == 24);
|
2017-04-03 14:45:06 +08:00
|
|
|
}
|
|
|
|
catch (const sol::error& err) {
|
2020-11-20 05:42:02 +08:00
|
|
|
std::cout << "Something went horribly wrong: thrown error"
|
|
|
|
<< "\n\t" << err.what() << std::endl;
|
2017-04-03 14:45:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OPTION 2 */
|
2017-08-07 14:07:21 +08:00
|
|
|
// Use the script_pass_on_error handler
|
2017-04-03 14:45:06 +08:00
|
|
|
// this simply passes through the protected_function_result,
|
|
|
|
// rather than throwing it or calling panic
|
|
|
|
// This will check code validity and also whether or not it runs well
|
|
|
|
{
|
2017-08-07 14:07:21 +08:00
|
|
|
sol::protected_function_result result = lua.script(code, sol::script_pass_on_error);
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(!result.valid());
|
2017-04-03 14:45:06 +08:00
|
|
|
if (!result.valid()) {
|
|
|
|
sol::error err = result;
|
|
|
|
sol::call_status status = result.status();
|
2020-11-20 05:42:02 +08:00
|
|
|
std::cout << "Something went horribly wrong: " << sol::to_string(status) << " error"
|
|
|
|
<< "\n\t" << err.what() << std::endl;
|
2017-04-03 14:45:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OPTION 3 */
|
|
|
|
// This is a lower-level, more explicit way to load code
|
|
|
|
// This explicitly loads the code, giving you access to any errors
|
|
|
|
// plus the load status
|
|
|
|
// then, it turns the loaded code into a sol::protected_function
|
|
|
|
// which is then called so that the code can run
|
|
|
|
// you can then check that too, for any errors
|
|
|
|
// The two previous approaches are recommended
|
|
|
|
{
|
|
|
|
sol::load_result loaded_chunk = lua.load(code);
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(!loaded_chunk.valid());
|
2017-04-03 14:45:06 +08:00
|
|
|
if (!loaded_chunk.valid()) {
|
|
|
|
sol::error err = loaded_chunk;
|
|
|
|
sol::load_status status = loaded_chunk.status();
|
2020-11-20 05:42:02 +08:00
|
|
|
std::cout << "Something went horribly wrong loading the code: " << sol::to_string(status) << " error"
|
|
|
|
<< "\n\t" << err.what() << std::endl;
|
2017-04-03 14:45:06 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Because the syntax is bad, this will never be reached
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(false);
|
2017-04-03 14:45:06 +08:00
|
|
|
// If there is a runtime error (lua GC memory error, nil access, etc.)
|
|
|
|
// it will be caught here
|
2020-11-20 05:42:02 +08:00
|
|
|
sol::protected_function script_func = loaded_chunk.get<sol::protected_function>();
|
2017-04-03 14:45:06 +08:00
|
|
|
sol::protected_function_result result = script_func();
|
|
|
|
if (!result.valid()) {
|
|
|
|
sol::error err = result;
|
|
|
|
sol::call_status status = result.status();
|
2020-11-20 05:42:02 +08:00
|
|
|
std::cout << "Something went horribly wrong running the code: " << sol::to_string(status) << " error"
|
|
|
|
<< "\n\t" << err.what() << std::endl;
|
2017-04-03 14:45:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:07:21 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
|
2017-04-03 14:45:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|