Jump to content

Module:User:Mr. Stradivarius/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 11:16, 22 October 2014 (proof of concept for complex error handling). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

function p.hasAnError(isCustomError)
	if isCustomError then
		error({msg = 'a custom error'})
	else
		return nil + 2
	end
end

function p._main(isCustomError)
	local function callHasAnError()
		return p.hasAnError(isCustomError)
	end
	local success, msg = xpcall(callHasAnError, function (x)
		if type(x) == 'table' then
			return 'This was a custom error, ' .. x.msg
		else
			error(x)
		end
	end)
	return msg
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local isCustomError = require('Module:Yesno')(args[1])
	return p._main(isCustomError)
end

return p