Add Lua errors to error dialog
parent
1455267c9e
commit
0d65ee878c
|
@ -60,11 +60,33 @@ function ui.update()
|
|||
|
||||
-- handle errors
|
||||
if gamedata ~= nil and gamedata.errormessage ~= nil then
|
||||
formspec = "size[12,3.2]" ..
|
||||
"textarea[1,1;10,2;;ERROR: " ..
|
||||
core.formspec_escape(gamedata.errormessage) ..
|
||||
";]"..
|
||||
"button[4.5,2.5;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
|
||||
local ar = gamedata.errormessage:split("\n")
|
||||
for i = 1, #ar do
|
||||
local text = ar[i]
|
||||
-- Hack to add word wrapping.
|
||||
-- TODO: Add engine support for wrapping in formspecs
|
||||
while #text > 80 do
|
||||
if formspec ~= "" then
|
||||
formspec = formspec .. ","
|
||||
end
|
||||
formspec = formspec .. core.formspec_escape(string.sub(text, 1, 79))
|
||||
text = string.sub(text, 80, #text)
|
||||
end
|
||||
if formspec ~= "" then
|
||||
formspec = formspec .. ","
|
||||
end
|
||||
formspec = formspec .. core.formspec_escape(text)
|
||||
end
|
||||
local error_title
|
||||
if string.find(gamedata.errormessage, "ModError") then
|
||||
error_title = fgettext("An error occured in a Lua script, such as a mod:")
|
||||
else
|
||||
error_title = fgettext("An error occured:")
|
||||
end
|
||||
formspec = "size[12,5]" ..
|
||||
"label[0.5,0;" .. error_title ..
|
||||
"]textlist[0.2,0.8;11.5,3.5;;" .. formspec ..
|
||||
"]button[4.5,4.6;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
|
||||
else
|
||||
local active_toplevel_ui_elements = 0
|
||||
for key,value in pairs(ui.childlist) do
|
||||
|
|
|
@ -119,14 +119,14 @@ ScriptApiBase::~ScriptApiBase()
|
|||
}
|
||||
|
||||
bool ScriptApiBase::loadMod(const std::string &script_path,
|
||||
const std::string &mod_name)
|
||||
const std::string &mod_name, std::string *error)
|
||||
{
|
||||
ModNameStorer mod_name_storer(getStack(), mod_name);
|
||||
|
||||
return loadScript(script_path);
|
||||
return loadScript(script_path, error);
|
||||
}
|
||||
|
||||
bool ScriptApiBase::loadScript(const std::string &script_path)
|
||||
bool ScriptApiBase::loadScript(const std::string &script_path, std::string *error)
|
||||
{
|
||||
verbosestream << "Loading and running script from " << script_path << std::endl;
|
||||
|
||||
|
@ -140,13 +140,14 @@ bool ScriptApiBase::loadScript(const std::string &script_path)
|
|||
}
|
||||
ok = ok && !lua_pcall(L, 0, 0, m_errorhandler);
|
||||
if (!ok) {
|
||||
errorstream << "========== ERROR FROM LUA ===========" << std::endl;
|
||||
errorstream << "Failed to load and run script from " << std::endl;
|
||||
errorstream << script_path << ":" << std::endl;
|
||||
errorstream << std::endl;
|
||||
errorstream << lua_tostring(L, -1) << std::endl;
|
||||
errorstream << std::endl;
|
||||
errorstream << "======= END OF ERROR FROM LUA ========" << std::endl;
|
||||
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
|
||||
return false;
|
||||
}
|
||||
|
@ -268,4 +269,3 @@ void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
|
|||
lua_remove(L, -2); // object_refs
|
||||
lua_remove(L, -2); // core
|
||||
}
|
||||
|
||||
|
|
|
@ -51,8 +51,8 @@ public:
|
|||
ScriptApiBase();
|
||||
virtual ~ScriptApiBase();
|
||||
|
||||
bool loadMod(const std::string &script_path, const std::string &mod_name);
|
||||
bool loadScript(const std::string &script_path);
|
||||
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);
|
||||
|
||||
/* object */
|
||||
void addObjectReference(ServerActiveObject *cobj);
|
||||
|
|
|
@ -239,11 +239,9 @@ Server::Server(
|
|||
m_mods = modconf.getMods();
|
||||
std::vector<ModSpec> unsatisfied_mods = modconf.getUnsatisfiedMods();
|
||||
// complain about mods with unsatisfied dependencies
|
||||
if(!modconf.isConsistent())
|
||||
{
|
||||
if(!modconf.isConsistent()) {
|
||||
for(std::vector<ModSpec>::iterator it = unsatisfied_mods.begin();
|
||||
it != unsatisfied_mods.end(); ++it)
|
||||
{
|
||||
it != unsatisfied_mods.end(); ++it) {
|
||||
ModSpec mod = *it;
|
||||
errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: ";
|
||||
for(std::set<std::string>::iterator dep_it = mod.unsatisfied_depends.begin();
|
||||
|
@ -259,8 +257,7 @@ Server::Server(
|
|||
std::vector<std::string> names = worldmt_settings.getNames();
|
||||
std::set<std::string> load_mod_names;
|
||||
for(std::vector<std::string>::iterator it = names.begin();
|
||||
it != names.end(); ++it)
|
||||
{
|
||||
it != names.end(); ++it) {
|
||||
std::string name = *it;
|
||||
if(name.compare(0,9,"load_mod_")==0 && worldmt_settings.getBool(name))
|
||||
load_mod_names.insert(name.substr(9));
|
||||
|
@ -272,8 +269,7 @@ Server::Server(
|
|||
for(std::vector<ModSpec>::iterator it = unsatisfied_mods.begin();
|
||||
it != unsatisfied_mods.end(); ++it)
|
||||
load_mod_names.erase((*it).name);
|
||||
if(!load_mod_names.empty())
|
||||
{
|
||||
if(!load_mod_names.empty()) {
|
||||
errorstream << "The following mods could not be found:";
|
||||
for(std::set<std::string>::iterator it = load_mod_names.begin();
|
||||
it != load_mod_names.end(); ++it)
|
||||
|
@ -296,10 +292,11 @@ Server::Server(
|
|||
m_script = new GameScripting(this);
|
||||
|
||||
std::string script_path = getBuiltinLuaPath() + DIR_DELIM "init.lua";
|
||||
std::string error_msg;
|
||||
|
||||
if (!m_script->loadMod(script_path, BUILTIN_MOD_NAME)) {
|
||||
throw ModError("Failed to load and run " + script_path);
|
||||
}
|
||||
if (!m_script->loadMod(script_path, BUILTIN_MOD_NAME, &error_msg))
|
||||
throw ModError("Failed to load and run " + script_path
|
||||
+ "\nError from Lua:\n" + error_msg);
|
||||
|
||||
// Print mods
|
||||
infostream << "Server: Loading mods: ";
|
||||
|
@ -314,18 +311,21 @@ Server::Server(
|
|||
i != m_mods.end(); i++) {
|
||||
const ModSpec &mod = *i;
|
||||
if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
|
||||
errorstream << "Error loading mod \"" << mod.name
|
||||
std::ostringstream err;
|
||||
err << "Error loading mod \"" << mod.name
|
||||
<< "\": mod_name does not follow naming conventions: "
|
||||
<< "Only chararacters [a-z0-9_] are allowed." << std::endl;
|
||||
throw ModError("Mod \"" + mod.name + "\" does not follow naming conventions.");
|
||||
errorstream << err.str().c_str();
|
||||
throw ModError(err.str());
|
||||
}
|
||||
std::string script_path = mod.path + DIR_DELIM "init.lua";
|
||||
infostream << " [" << padStringRight(mod.name, 12) << "] [\""
|
||||
<< script_path << "\"]" << std::endl;
|
||||
if (!m_script->loadMod(script_path, mod.name)) {
|
||||
if (!m_script->loadMod(script_path, mod.name, &error_msg)) {
|
||||
errorstream << "Server: Failed to load and run "
|
||||
<< script_path << std::endl;
|
||||
throw ModError("Failed to load and run " + script_path);
|
||||
throw ModError("Failed to load and run " + script_path
|
||||
+ "\nError from Lua:\n" + error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue