diff options
author | ShadowNinja <shadowninja@minetest.net> | 2015-10-29 14:48:10 -0400 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2015-10-31 13:28:58 -0400 |
commit | 9269a0ecc7267822bc5ac5af95ad4977bdc94fec (patch) | |
tree | 1cf8639cc0a6b367670ac673780f321f85b467be /src/script/cpp_api | |
parent | b872df6ef6a15ae4624b35ea7b8960bc24da1128 (diff) | |
download | minetest-9269a0ecc7267822bc5ac5af95ad4977bdc94fec.tar.gz minetest-9269a0ecc7267822bc5ac5af95ad4977bdc94fec.tar.bz2 minetest-9269a0ecc7267822bc5ac5af95ad4977bdc94fec.zip |
Fix server crashing on Lua errors
Previously, the server called FATAL_ERROR when a Lua error occured.
This caused a (mostly useless) core dump.
The server now simply throws an exception, which is caught and printed before
exiting with a non-zero return value.
This also fixes a number of instances where errors were logged multiple times.
Diffstat (limited to 'src/script/cpp_api')
-rw-r--r-- | src/script/cpp_api/s_async.cpp | 8 | ||||
-rw-r--r-- | src/script/cpp_api/s_base.cpp | 20 | ||||
-rw-r--r-- | src/script/cpp_api/s_base.h | 6 |
3 files changed, 16 insertions, 18 deletions
diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 171632633..9bf3fcf49 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -243,8 +243,12 @@ void* AsyncWorkerThread::run() lua_State *L = getStack(); std::string script = getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua"; - if (!loadScript(script)) { - FATAL_ERROR("execution of async base environment failed!"); + try { + loadScript(script); + } catch (const ModError &e) { + errorstream << "Execution of async base environment failed: " + << e.what() << std::endl; + FATAL_ERROR("Execution of async base environment failed"); } int error_handler = PUSH_ERROR_HANDLER(L); diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 78b70e499..b40d31533 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -119,15 +119,15 @@ ScriptApiBase::~ScriptApiBase() lua_close(m_luastack); } -bool ScriptApiBase::loadMod(const std::string &script_path, - const std::string &mod_name, std::string *error) +void ScriptApiBase::loadMod(const std::string &script_path, + const std::string &mod_name) { ModNameStorer mod_name_storer(getStack(), mod_name); - return loadScript(script_path, error); + loadScript(script_path); } -bool ScriptApiBase::loadScript(const std::string &script_path, std::string *error) +void ScriptApiBase::loadScript(const std::string &script_path) { verbosestream << "Loading and running script from " << script_path << std::endl; @@ -144,17 +144,11 @@ bool ScriptApiBase::loadScript(const std::string &script_path, std::string *erro ok = ok && !lua_pcall(L, 0, 0, error_handler); if (!ok) { std::string error_msg = lua_tostring(L, -1); - if (error) - *error = error_msg; - errorstream << "========== ERROR FROM LUA ===========" << std::endl - << "Failed to load and run script from " << std::endl - << script_path << ":" << std::endl << std::endl - << error_msg << std::endl << std::endl - << "======= END OF ERROR FROM LUA ========" << std::endl; - lua_pop(L, 1); // Pop error message from stack + lua_pop(L, 2); // Pop error message and error handler + throw ModError("Failed to load and run script from " + + script_path + ":\n" + error_msg); } lua_pop(L, 1); // Pop error handler - return ok; } // Push the list of callbacks (a lua table). diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index d490f7dfd..20f4bc11b 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -63,9 +63,9 @@ public: ScriptApiBase(); virtual ~ScriptApiBase(); - bool loadMod(const std::string &script_path, const std::string &mod_name, - std::string *error=NULL); - bool loadScript(const std::string &script_path, std::string *error=NULL); + // These throw a ModError on failure + void loadMod(const std::string &script_path, const std::string &mod_name); + void loadScript(const std::string &script_path); void runCallbacksRaw(int nargs, RunCallbacksMode mode, const char *fxn); |