Created
April 2, 2014 20:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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