Skip to content

Commit f3d83a4

Browse files
committedMar 15, 2014
Add more informative error messages for inventory and item method errors
1 parent 31fe72d commit f3d83a4

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed
 

‎src/script/cpp_api/s_inventory.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int ScriptApiDetached::detached_inventory_AllowMove(
5454
if(lua_pcall(L, 7, 1, errorhandler))
5555
scriptError();
5656
if(!lua_isnumber(L, -1))
57-
throw LuaError("allow_move should return a number");
57+
throw LuaError("allow_move should return a number. name=" + name);
5858
int ret = luaL_checkinteger(L, -1);
5959
lua_pop(L, 2); // Pop integer and error handler
6060
return ret;
@@ -86,7 +86,7 @@ int ScriptApiDetached::detached_inventory_AllowPut(
8686
if(lua_pcall(L, 5, 1, errorhandler))
8787
scriptError();
8888
if(!lua_isnumber(L, -1))
89-
throw LuaError("allow_put should return a number");
89+
throw LuaError("allow_put should return a number. name=" + name);
9090
int ret = luaL_checkinteger(L, -1);
9191
lua_pop(L, 2); // Pop integer and error handler
9292
return ret;
@@ -118,7 +118,7 @@ int ScriptApiDetached::detached_inventory_AllowTake(
118118
if(lua_pcall(L, 5, 1, errorhandler))
119119
scriptError();
120120
if(!lua_isnumber(L, -1))
121-
throw LuaError("allow_take should return a number");
121+
throw LuaError("allow_take should return a number. name=" + name);
122122
int ret = luaL_checkinteger(L, -1);
123123
lua_pop(L, 2); // Pop integer and error handler
124124
return ret;

‎src/script/cpp_api/s_item.cpp

+35-10
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
4747
pushFloatPos(L, pos);
4848
if(lua_pcall(L, 3, 1, errorhandler))
4949
scriptError();
50-
if(!lua_isnil(L, -1))
51-
item = read_item(L,-1, getServer());
50+
if(!lua_isnil(L, -1)) {
51+
try {
52+
item = read_item(L,-1, getServer());
53+
} catch (LuaError &e) {
54+
throw LuaError(std::string(e.what()) + ". item=" + item.name);
55+
}
56+
}
5257
lua_pop(L, 2); // Pop item and error handler
5358
return true;
5459
}
@@ -71,8 +76,13 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
7176
pushPointedThing(pointed);
7277
if(lua_pcall(L, 3, 1, errorhandler))
7378
scriptError();
74-
if(!lua_isnil(L, -1))
75-
item = read_item(L,-1, getServer());
79+
if(!lua_isnil(L, -1)) {
80+
try {
81+
item = read_item(L,-1, getServer());
82+
} catch (LuaError &e) {
83+
throw LuaError(std::string(e.what()) + ". item=" + item.name);
84+
}
85+
}
7686
lua_pop(L, 2); // Pop item and error handler
7787
return true;
7888
}
@@ -95,8 +105,13 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
95105
pushPointedThing(pointed);
96106
if(lua_pcall(L, 3, 1, errorhandler))
97107
scriptError();
98-
if(!lua_isnil(L, -1))
99-
item = read_item(L,-1, getServer());
108+
if(!lua_isnil(L, -1)) {
109+
try {
110+
item = read_item(L,-1, getServer());
111+
} catch (LuaError &e) {
112+
throw LuaError(std::string(e.what()) + ". item=" + item.name);
113+
}
114+
}
100115
lua_pop(L, 2); // Pop item and error handler
101116
return true;
102117
}
@@ -123,8 +138,13 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
123138
InvRef::create(L, craft_inv);
124139
if(lua_pcall(L, 4, 1, errorhandler))
125140
scriptError();
126-
if(!lua_isnil(L, -1))
127-
item = read_item(L,-1, getServer());
141+
if(!lua_isnil(L, -1)) {
142+
try {
143+
item = read_item(L,-1, getServer());
144+
} catch (LuaError &e) {
145+
throw LuaError(std::string(e.what()) + ". item=" + item.name);
146+
}
147+
}
128148
lua_pop(L, 2); // Pop item and error handler
129149
return true;
130150
}
@@ -151,8 +171,13 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
151171
InvRef::create(L, craft_inv);
152172
if(lua_pcall(L, 4, 1, errorhandler))
153173
scriptError();
154-
if(!lua_isnil(L, -1))
155-
item = read_item(L,-1, getServer());
174+
if(!lua_isnil(L, -1)) {
175+
try {
176+
item = read_item(L,-1, getServer());
177+
} catch (LuaError &e) {
178+
throw LuaError(std::string(e.what()) + ". item=" + item.name);
179+
}
180+
}
156181
lua_pop(L, 2); // Pop item and error handler
157182
return true;
158183
}

0 commit comments

Comments
 (0)
Please sign in to comment.