Skip to content

Instantly share code, notes, and snippets.

@est31
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save est31/e84180467a76f8badb42 to your computer and use it in GitHub Desktop.
Save est31/e84180467a76f8badb42 to your computer and use it in GitHub Desktop.
Lua error reporting
diff --git a/builtin/game/register.lua b/builtin/game/register.lua
index d0e04bf..002c8b2 100644
--- a/builtin/game/register.lua
+++ b/builtin/game/register.lua
@@ -10,6 +10,21 @@ core.register_item_raw = nil
local register_alias_raw = core.register_alias_raw
core.register_alias_raw = nil
+local function make_func_safe(func)
+ return function(...)
+ local arg = {...}
+ local worked, result = xpcall(function () return func(unpack(arg)) end,
+ function(err)
+ return debug.traceback(err, 1)
+ end)
+ if worked then
+ return result
+ else
+ print(result)
+ assert(false) -- still error, so that we can be sure we fail
+ end
+ end
+end
+
--
-- Item / entity / ABM registration functions
--
@@ -154,6 +169,11 @@ function core.register_item(name, itemdef)
-- Disable all further modifications
getmetatable(itemdef).__newindex = {}
+ for idx, obj in pairs(itemdef) do
+ if type(obj) == "function" then
+ itemdef[idx] = make_func_safe(obj)
+ end
+ end
--core.log("Registering item: " .. itemdef.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment