Jump to content

Module:Protect: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
New module
 
Tweak errFormat behavior
Line 1: Line 1:
local function wrapMessage(errMessage)
return '<span class="error">' .. errMessage .. '</span>'
end

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

local function processResult(errFormat, success, ...)
local function processResult(errFormat, success, ...)
if not success then
if not success then
Line 13: Line 7:


local function protect(func, errFormat, raw)
local function protect(func, errFormat, raw)
if not errFormat then
errFormat = errFormat or 'Error: %s'
if not raw then
errFormat = defaultFormat
errFormat = '<strong class="error">' .. errFormat .. '</strong>'
elseif not raw then
errFormat = wrapMessage(errFormat)
end
end

Revision as of 15:21, 19 August 2018

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)
	errFormat = errFormat or 'Error: %s'
	if not raw then
		errFormat = '<strong class="error">' .. errFormat .. '</strong>'
	end
	
	return function (...)
		return processResult(errFormat, pcall(func, ...))
	end
end

return protect