Fix script error reporting a bit
authorPerttu Ahola <celeron55@gmail.com>
Fri, 2 Dec 2011 20:49:54 +0000 (22:49 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Fri, 2 Dec 2011 20:49:54 +0000 (22:49 +0200)
src/script.cpp
src/script.h
src/scriptapi.cpp
src/scriptapi.h
src/server.cpp

index 16d8030d6eef7f712e3b7ccfb0e2df5e9d17300a..5a6c9802636a66108fffcfa08cfad46f3aa43179 100644 (file)
@@ -35,10 +35,11 @@ void script_error(lua_State *L, const char *fmt, ...)
 {
        va_list argp;
        va_start(argp, fmt);
-       vfprintf(stderr, fmt, argp);
+       char buf[10000];
+       vsnprintf(buf, 10000, fmt, argp);
        va_end(argp);
-       lua_close(L);
-       exit(EXIT_FAILURE);
+       //errorstream<<"SCRIPT ERROR: "<<buf;
+       throw LuaError(buf);
 }
 
 bool script_load(lua_State *L, const char *path)
index ce697bc50ba931b07d96e760e9bcd4df74980331..6da95acf73fef7fe77773d19f61f7a1c9356a7f9 100644 (file)
@@ -20,8 +20,27 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef SCRIPT_HEADER
 #define SCRIPT_HEADER
 
+#include <exception>
+#include <string>
+
+class LuaError : public std::exception
+{
+public:
+       LuaError(const std::string &s)
+       {
+               m_s = "LuaError: ";
+               m_s += s;
+       }
+       virtual ~LuaError() throw()
+       {}
+       virtual const char * what() const throw()
+       {
+               return m_s.c_str();
+       }
+       std::string m_s;
+};
+
 typedef struct lua_State lua_State;
-//#include <string>
 
 lua_State* script_init();
 void script_deinit(lua_State *L);
index cb26fa47297a8c70692046bdba44d3544b0c66c4..db6b7e86e22ce27addaa58793eae9ed582c904ee 100644 (file)
@@ -1101,7 +1101,10 @@ static int l_register_craft(lua_State *L)
                                width = colcount;
                        } else {
                                if(colcount != width){
-                                       script_error(L, "error: %s\n", "Invalid crafting recipe");
+                                       std::string error;
+                                       error += "Invalid crafting recipe (output=\""
+                                                       + output + "\")";
+                                       throw LuaError(error);
                                }
                        }
                        // removes value, keeps key for next iteration
@@ -2469,6 +2472,21 @@ void scriptapi_export(lua_State *L, Server *server)
        ObjectRef::Register(L);
 }
 
+bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
+               const std::string &modname)
+{
+       bool success = false;
+
+       try{
+               success = script_load(L, scriptpath.c_str());
+       }
+       catch(LuaError &e){
+               errorstream<<"Error loading mod: "<<e.what()<<std::endl;
+       }
+
+       return success;
+}
+
 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
 {
        realitycheck(L);
index 33b7954154f0938e989c3c1263c8b369cd1ad1bb..d107b15ce82904696d53b173ca8e93c5f8822495 100644 (file)
@@ -34,6 +34,8 @@ struct PointedThing;
 class ServerRemotePlayer;
 
 void scriptapi_export(lua_State *L, Server *server);
+bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
+               const std::string &modname);
 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env);
 
 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
index b8bdd883060c7c5ed7acfd7065774581898c6a0a..e345d8811ff28155225f0642c6329d41a9151769 100644 (file)
@@ -982,7 +982,7 @@ Server::Server(
        if(!success){
                errorstream<<"Server: Failed to load and run "
                                <<builtinpath<<std::endl;
-               assert(0);
+               exit(1);
        }
        // Load and run "mod" scripts
        core::list<ModSpec> mods = getMods(m_modspaths);
@@ -991,11 +991,11 @@ Server::Server(
                ModSpec mod = *i;
                infostream<<"Server: Loading mod \""<<mod.name<<"\""<<std::endl;
                std::string scriptpath = mod.path + DIR_DELIM + "init.lua";
-               bool success = script_load(m_lua, scriptpath.c_str());
+               bool success = scriptapi_loadmod(m_lua, scriptpath, mod.name);
                if(!success){
                        errorstream<<"Server: Failed to load and run "
                                        <<scriptpath<<std::endl;
-                       assert(0);
+                       exit(1);
                }
        }