Module:Protect

Revision as of 12:44, 19 August 2018 by wikipedia>Petr Matas (New module)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Protect/doc

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