Skip to content

Commit

Permalink
Add more informative error messages for inventory and item method errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowNinja committed Mar 15, 2014
1 parent 31fe72d commit f3d83a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/script/cpp_api/s_inventory.cpp
Expand Up @@ -54,7 +54,7 @@ int ScriptApiDetached::detached_inventory_AllowMove(
if(lua_pcall(L, 7, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
throw LuaError("allow_move should return a number");
throw LuaError("allow_move should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
Expand Down Expand Up @@ -86,7 +86,7 @@ int ScriptApiDetached::detached_inventory_AllowPut(
if(lua_pcall(L, 5, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
throw LuaError("allow_put should return a number");
throw LuaError("allow_put should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
Expand Down Expand Up @@ -118,7 +118,7 @@ int ScriptApiDetached::detached_inventory_AllowTake(
if(lua_pcall(L, 5, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
throw LuaError("allow_take should return a number");
throw LuaError("allow_take should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
Expand Down
45 changes: 35 additions & 10 deletions src/script/cpp_api/s_item.cpp
Expand Up @@ -47,8 +47,13 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
pushFloatPos(L, pos);
if(lua_pcall(L, 3, 1, errorhandler))
scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
if(!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
Expand All @@ -71,8 +76,13 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
pushPointedThing(pointed);
if(lua_pcall(L, 3, 1, errorhandler))
scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
if(!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
Expand All @@ -95,8 +105,13 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
pushPointedThing(pointed);
if(lua_pcall(L, 3, 1, errorhandler))
scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
if(!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
Expand All @@ -123,8 +138,13 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
InvRef::create(L, craft_inv);
if(lua_pcall(L, 4, 1, errorhandler))
scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
if(!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
Expand All @@ -151,8 +171,13 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
InvRef::create(L, craft_inv);
if(lua_pcall(L, 4, 1, errorhandler))
scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
if(!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
Expand Down

0 comments on commit f3d83a4

Please sign in to comment.