From e80ccdfac750fdd318eecb35b5f48a3d251ec7ec Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 9 Jun 2008 06:49:58 +0000 Subject: Made output accumulation in a buffer and write in the end, thus enabling the corretc outputting of the error box if neccessary. --- server/src/luaquerymapper.cc | 79 +++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 34 deletions(-) (limited to 'server/src/luaquerymapper.cc') diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc index df25778..5b66a35 100644 --- a/server/src/luaquerymapper.cc +++ b/server/src/luaquerymapper.cc @@ -28,6 +28,8 @@ #include +#include "exception.h" + static std::string loadresultstring(QueryResult &res, std::string group = "") { std::string s; @@ -55,29 +57,24 @@ LUAQueryMapper::LUAQueryMapper(QueryResult &res) { L = luaL_newstate(); if(L == NULL) { - // throw LUADataParserException("Could not create LUA state."); + error("Could not create LUA state."); + return; } luaL_openlibs(L); std::string preload = loadresultstring(res); - int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload"); - switch(s) { - case 0: //no errors; - break; - case LUA_ERRSYNTAX: //syntax error during pre-compilation; - case LUA_ERRMEM: //memory allocation error. - case LUA_ERRFILE: //cannot open/read the file. - //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); - break; - default: - //throw LUADataParserException("Unknown return value of luaL_loadfile."); - break; + if(luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload")) { + error(lua_tostring(L, lua_gettop(L))); + return; } // Run program (init) - lua_pcall(L, 0, LUA_MULTRET, 0); + if(lua_pcall(L, 0, LUA_MULTRET, 0)) { + error(lua_tostring(L, lua_gettop(L))); + return; + } clean_top = lua_gettop(L); } @@ -91,47 +88,61 @@ Value LUAQueryMapper::map(const std::string &mapper) { Value v; + if(L == NULL) { + error("LUA state not initialized!"); + return v; + } + if(mapper == "") { - printf("Empty LUA mapper detected!\n"); - v.timestamp = 0; - v.value = ""; + error("Empty LUA mapper detected in " + mapper); + return v; } - int s = luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper"); - switch(s) { - case 0: //no errors; - break; - case LUA_ERRSYNTAX: //syntax error during pre-compilation; - case LUA_ERRMEM: //memory allocation error. - case LUA_ERRFILE: //cannot open/read the file. - //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); - break; - default: - //throw LUADataParserException("Unknown return value of luaL_loadfile."); - break; + // Load the mapper + if(luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper")) { + error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper); + return v; } // Run the loaded code - lua_pcall(L, 0, LUA_MULTRET, 0); + if(lua_pcall(L, 0, LUA_MULTRET, 0)) { + error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper); + return v; + } // Check if app messed up the stack. if(lua_gettop(L) != clean_top + 2) { - printf("LUA mapper messed up the stack (wrong number of return values)!\n"); + error("Wrong number of return values in " + mapper); lua_pop(L, lua_gettop(L) - clean_top); - Value v; - v.timestamp = 0; - v.value = ""; return v; } + // Check if the types are right + if(lua_isnumber(L, lua_gettop(L)) == false) { + error("Timestamp is not an integer in " + mapper); + lua_pop(L, 2); + return v; + } v.timestamp = lua_tointeger(L, lua_gettop(L)); lua_pop(L, 1); + + // Check if the types are right + if(lua_isstring(L, lua_gettop(L)) == false) { + error("Value is not a string in " + mapper); + lua_pop(L, 1); + return v; + } v.value = lua_tostring(L, lua_gettop(L)); lua_pop(L, 1); return v; } +void LUAQueryMapper::error(std::string message) +{ + throw Exception("ERROR in LUAQueryMapper: " + message); +} + #ifdef TEST_LUAQUERYMAPPER #include "queryhandler.h" -- cgit v1.2.3