Skip to content

Instantly share code, notes, and snippets.

@britzl
Created April 2, 2014 20:08
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 britzl/9942087 to your computer and use it in GitHub Desktop.
Save britzl/9942087 to your computer and use it in GitHub Desktop.
Example of how to wrap script entry points to be able to do custom error handling
local function on_error(err)
print("In custom error handler:", err)
-- something went wrong, handle error here
-- report to client health for instance
-- print(debug.traceback())
-- dump tables
end
local function do_something(a_number)
-- if this check fails it will be caught by the error handler
assert(type(a_number) == "number", "Expected a number")
-- the below code would also fail and get caught by the error handler
print(nil + "some string")
-- and this
error("Oops")
end
local function _init(self)
-- do init stuff here
do_something("sending a string when number is expected")
end
function init(self)
-- you can't pass arguments to xpcall so we need to wrap the function
-- we could do the same for on_input(), on_message(), update() and final()
local ok, result = xpcall(function() _init(self) end, on_error)
end
init({})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment