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 14:14, 22 October 2014 (test error objects with __tostring metamethods). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local Err = {}
Err.__index = Err

function Err.new(msg, help, level)
	local obj = setmetatable({}, Err)
	obj.msg = msg
	obj.help = help
	level = tonumber(level)
	if level and level > 0 then
		level = level + 1
	end
	error(obj, level)
end

function Err.__tostring()
	if help then
		return string.format('Error: %s ([[WP:FOO#%s|help]]).', self.msg, self.help)
	else
		return string.format('Error: %s.', self.msg)
	end
end

local p = {}

function p.theError(n)
	if n == -2 then
		Err.new('this is an error object', 'help section', 2)
	elseif n == -1 then
		error('this has no explicit level')
	elseif n == 0 then
		error('this is level 0', 0)
	elseif n == 1 then
		error('this is explicit level 1', 1)
	elseif n == 2 then
		error('this is level 2', 2)
	else
		error('this is level 3', 3)
	end
end

function p._main(n)
	return p.theError(tonumber(n))
end

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

return p