Module:Protect: Difference between revisions

    From Nonbinary Wiki
    (New module)
    (No difference)

    Revision as of 12:44, 19 August 2018

    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