Jump to content

Module:Protect

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Petr Matas (talk | contribs) at 12:44, 19 August 2018 (New module). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local function wrapMessage(errMessage)
	return '<span class="error">' .. errMessage .. '</span>'
end

local defaultFormat = wrapMessage('Error: %s')

local function processResult(errFormat, success, ...)
	if not success then
		return string.format(errFormat, tostring(... or '(no message)'))
	end
	return ...
end

local function protect(func, errFormat, raw)
	if not errFormat then
		errFormat = defaultFormat
	elseif not raw then
		errFormat = wrapMessage(errFormat)
	end
	
	return function (...)
		return processResult(errFormat, pcall(func, ...))
	end
end

return protect