Module:Error

From Halopedia, the Halo wiki

Documentation for this module may be created at ModuleDoc:Error

local utils = require( 'Module:Utils' )

Error = {
	message = 'There was an error with a template. Are the parameters all correct?',
	etype = 'Template',
	category = 'Pages containing template errors',
	template = nil
}

function Error:new( obj )
	obj = obj or {}
	setmetatable( obj, self )
	self.__index = self
	return obj
end

function Error:withMsg( message )
	self.message = tostring( message or self.message )
	return self
end

function Error:withCat( category )
	self.category = tostring( category or self.category )
	return self
end

function Error:withoutCat()
	self.category = nil
	return self
end

function Error:ofType( etype )
	self.etype = tostring( etype or self.etype )
	return self
end

function Error:thrownBy( template )
	self.template = template or self.template
	return self
end

function Error:__tostring()
	-- Generate error message based on provided info
	local text = [[''']]
		.. mw.ustring.upper( self.etype )
		.. [[ ERROR:''' ]]
		.. message
	
	-- If the template that threw the error has been provided, display it
	if self.template then
		text = text .. ' (See [[Template:' .. tostring( self.template ) .. ']])'
	end
	
	-- Add the styling
	text = [[<span class="error halopedia-template-error" style="color: red;">]]
		.. wikitext
		.. [[</span>]]
	
	-- If this function is run, we assume that the error has not just been
	-- created, but also thrown. Thus, we add the warning to the header.
	mw.addWarning( text )
	
	-- Add the category, if present
	if self.category then
		text = text .. '[[Category:' .. self.category .. ']]'
	end
	
	return text
end

local p = {}

-- Adapter for the Halopedia Lua utils.error function, for use in templates

function p.error( frame )
	return utils.error( frame.args[1], frame.args[2], frame.args[3], frame.args[4] )
end

return p