Module:Protect: Difference between revisions

399 bytes added ,  2 years ago
m
5 revisions imported from wikipedia:Module:Protect
(Tweak errFormat behavior)
m (5 revisions imported from wikipedia:Module:Protect)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local function processResult(errFormat, success, ...)
local function processResult(options, success, ...)
if not success then
if not success then
return string.format(errFormat, tostring(... or '(no message)'))
local message = tostring(... or '(no message)')
if options.removeLocation then
message = string.gsub(message, '^Module:[^:]+:%d+: ', '', 1)
end
return string.format(options.errFormat, message)
end
end
return ...
return ...
end
end


local function protect(func, errFormat, raw)
local function protect(func, errFormat, options)
errFormat = errFormat or 'Error: %s'
if type(errFormat) == 'table' then
if not raw then
options = options or errFormat
errFormat = '<strong class="error">' .. errFormat .. '</strong>'
errFormat = nil
end
end
options = mw.clone(options) or {}
options.errFormat = errFormat or options.errFormat or 'Error: %s'
if not options.raw then
options.errFormat = '<strong class="error">' .. options.errFormat .. '</strong>'
end
options.removeLocation = options.removeLocation == nil or options.removeLocation
return function (...)
return function (...)
return processResult(errFormat, pcall(func, ...))
return processResult(options, pcall(func, ...))
end
end
end
end


return protect
return protect