Skip to content

Commit 0d65ee8

Browse files
rubenwardyest31
authored andcommittedJun 29, 2015
Add Lua errors to error dialog
1 parent 1455267 commit 0d65ee8

File tree

4 files changed

+60
-38
lines changed

4 files changed

+60
-38
lines changed
 

Diff for: ‎builtin/fstk/ui.lua

+31-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ui.default = nil
2323
function ui.add(child)
2424
--TODO check child
2525
ui.childlist[child.name] = child
26-
26+
2727
return child.name
2828
end
2929

@@ -33,7 +33,7 @@ function ui.delete(child)
3333
if ui.childlist[child.name] == nil then
3434
return false
3535
end
36-
36+
3737
ui.childlist[child.name] = nil
3838
return true
3939
end
@@ -60,11 +60,33 @@ function ui.update()
6060

6161
-- handle errors
6262
if gamedata ~= nil and gamedata.errormessage ~= nil then
63-
formspec = "size[12,3.2]" ..
64-
"textarea[1,1;10,2;;ERROR: " ..
65-
core.formspec_escape(gamedata.errormessage) ..
66-
";]"..
67-
"button[4.5,2.5;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
63+
local ar = gamedata.errormessage:split("\n")
64+
for i = 1, #ar do
65+
local text = ar[i]
66+
-- Hack to add word wrapping.
67+
-- TODO: Add engine support for wrapping in formspecs
68+
while #text > 80 do
69+
if formspec ~= "" then
70+
formspec = formspec .. ","
71+
end
72+
formspec = formspec .. core.formspec_escape(string.sub(text, 1, 79))
73+
text = string.sub(text, 80, #text)
74+
end
75+
if formspec ~= "" then
76+
formspec = formspec .. ","
77+
end
78+
formspec = formspec .. core.formspec_escape(text)
79+
end
80+
local error_title
81+
if string.find(gamedata.errormessage, "ModError") then
82+
error_title = fgettext("An error occured in a Lua script, such as a mod:")
83+
else
84+
error_title = fgettext("An error occured:")
85+
end
86+
formspec = "size[12,5]" ..
87+
"label[0.5,0;" .. error_title ..
88+
"]textlist[0.2,0.8;11.5,3.5;;" .. formspec ..
89+
"]button[4.5,4.6;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
6890
else
6991
local active_toplevel_ui_elements = 0
7092
for key,value in pairs(ui.childlist) do
@@ -77,7 +99,7 @@ function ui.update()
7799
end
78100
end
79101
end
80-
102+
81103
-- no need to show addons if there ain't a toplevel element
82104
if (active_toplevel_ui_elements > 0) then
83105
for key,value in pairs(ui.childlist) do
@@ -127,7 +149,7 @@ end
127149

128150
--------------------------------------------------------------------------------
129151
function ui.handle_events(event)
130-
152+
131153
for key,value in pairs(ui.childlist) do
132154

133155
if value.handle_events ~= nil then

Diff for: ‎src/script/cpp_api/s_base.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ ScriptApiBase::~ScriptApiBase()
119119
}
120120

121121
bool ScriptApiBase::loadMod(const std::string &script_path,
122-
const std::string &mod_name)
122+
const std::string &mod_name, std::string *error)
123123
{
124124
ModNameStorer mod_name_storer(getStack(), mod_name);
125125

126-
return loadScript(script_path);
126+
return loadScript(script_path, error);
127127
}
128128

129-
bool ScriptApiBase::loadScript(const std::string &script_path)
129+
bool ScriptApiBase::loadScript(const std::string &script_path, std::string *error)
130130
{
131131
verbosestream << "Loading and running script from " << script_path << std::endl;
132132

@@ -140,13 +140,14 @@ bool ScriptApiBase::loadScript(const std::string &script_path)
140140
}
141141
ok = ok && !lua_pcall(L, 0, 0, m_errorhandler);
142142
if (!ok) {
143-
errorstream << "========== ERROR FROM LUA ===========" << std::endl;
144-
errorstream << "Failed to load and run script from " << std::endl;
145-
errorstream << script_path << ":" << std::endl;
146-
errorstream << std::endl;
147-
errorstream << lua_tostring(L, -1) << std::endl;
148-
errorstream << std::endl;
149-
errorstream << "======= END OF ERROR FROM LUA ========" << std::endl;
143+
std::string error_msg = lua_tostring(L, -1);
144+
if (error)
145+
(*error) = error_msg;
146+
errorstream << "========== ERROR FROM LUA ===========" << std::endl
147+
<< "Failed to load and run script from " << std::endl
148+
<< script_path << ":" << std::endl << std::endl
149+
<< error_msg << std::endl << std::endl
150+
<< "======= END OF ERROR FROM LUA ========" << std::endl;
150151
lua_pop(L, 1); // Pop error message from stack
151152
return false;
152153
}
@@ -268,4 +269,3 @@ void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
268269
lua_remove(L, -2); // object_refs
269270
lua_remove(L, -2); // core
270271
}
271-

Diff for: ‎src/script/cpp_api/s_base.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class ScriptApiBase {
5151
ScriptApiBase();
5252
virtual ~ScriptApiBase();
5353

54-
bool loadMod(const std::string &script_path, const std::string &mod_name);
55-
bool loadScript(const std::string &script_path);
54+
bool loadMod(const std::string &script_path, const std::string &mod_name, std::string *error=NULL);
55+
bool loadScript(const std::string &script_path, std::string *error=NULL);
5656

5757
/* object */
5858
void addObjectReference(ServerActiveObject *cobj);

Diff for: ‎src/server.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,9 @@ Server::Server(
239239
m_mods = modconf.getMods();
240240
std::vector<ModSpec> unsatisfied_mods = modconf.getUnsatisfiedMods();
241241
// complain about mods with unsatisfied dependencies
242-
if(!modconf.isConsistent())
243-
{
242+
if(!modconf.isConsistent()) {
244243
for(std::vector<ModSpec>::iterator it = unsatisfied_mods.begin();
245-
it != unsatisfied_mods.end(); ++it)
246-
{
244+
it != unsatisfied_mods.end(); ++it) {
247245
ModSpec mod = *it;
248246
errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: ";
249247
for(std::set<std::string>::iterator dep_it = mod.unsatisfied_depends.begin();
@@ -259,8 +257,7 @@ Server::Server(
259257
std::vector<std::string> names = worldmt_settings.getNames();
260258
std::set<std::string> load_mod_names;
261259
for(std::vector<std::string>::iterator it = names.begin();
262-
it != names.end(); ++it)
263-
{
260+
it != names.end(); ++it) {
264261
std::string name = *it;
265262
if(name.compare(0,9,"load_mod_")==0 && worldmt_settings.getBool(name))
266263
load_mod_names.insert(name.substr(9));
@@ -272,8 +269,7 @@ Server::Server(
272269
for(std::vector<ModSpec>::iterator it = unsatisfied_mods.begin();
273270
it != unsatisfied_mods.end(); ++it)
274271
load_mod_names.erase((*it).name);
275-
if(!load_mod_names.empty())
276-
{
272+
if(!load_mod_names.empty()) {
277273
errorstream << "The following mods could not be found:";
278274
for(std::set<std::string>::iterator it = load_mod_names.begin();
279275
it != load_mod_names.end(); ++it)
@@ -296,15 +292,16 @@ Server::Server(
296292
m_script = new GameScripting(this);
297293

298294
std::string script_path = getBuiltinLuaPath() + DIR_DELIM "init.lua";
295+
std::string error_msg;
299296

300-
if (!m_script->loadMod(script_path, BUILTIN_MOD_NAME)) {
301-
throw ModError("Failed to load and run " + script_path);
302-
}
297+
if (!m_script->loadMod(script_path, BUILTIN_MOD_NAME, &error_msg))
298+
throw ModError("Failed to load and run " + script_path
299+
+ "\nError from Lua:\n" + error_msg);
303300

304301
// Print mods
305302
infostream << "Server: Loading mods: ";
306303
for(std::vector<ModSpec>::iterator i = m_mods.begin();
307-
i != m_mods.end(); i++){
304+
i != m_mods.end(); i++) {
308305
const ModSpec &mod = *i;
309306
infostream << mod.name << " ";
310307
}
@@ -314,18 +311,21 @@ Server::Server(
314311
i != m_mods.end(); i++) {
315312
const ModSpec &mod = *i;
316313
if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
317-
errorstream << "Error loading mod \"" << mod.name
314+
std::ostringstream err;
315+
err << "Error loading mod \"" << mod.name
318316
<< "\": mod_name does not follow naming conventions: "
319317
<< "Only chararacters [a-z0-9_] are allowed." << std::endl;
320-
throw ModError("Mod \"" + mod.name + "\" does not follow naming conventions.");
318+
errorstream << err.str().c_str();
319+
throw ModError(err.str());
321320
}
322321
std::string script_path = mod.path + DIR_DELIM "init.lua";
323322
infostream << " [" << padStringRight(mod.name, 12) << "] [\""
324323
<< script_path << "\"]" << std::endl;
325-
if (!m_script->loadMod(script_path, mod.name)) {
324+
if (!m_script->loadMod(script_path, mod.name, &error_msg)) {
326325
errorstream << "Server: Failed to load and run "
327326
<< script_path << std::endl;
328-
throw ModError("Failed to load and run " + script_path);
327+
throw ModError("Failed to load and run " + script_path
328+
+ "\nError from Lua:\n" + error_msg);
329329
}
330330
}
331331

0 commit comments

Comments
 (0)
Please sign in to comment.