Module:Error: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:


function p.Error(frame)
function p.Error(frame)
local args = frame:getParent().args
local args = frame.args
if frame.args[1] then
args = frame.args
end
return p.printError(args[1], args[2])
return p.printError(args[1], args[2])
end
end


function p.Warn(frame)
function p.Warn(frame)
local args = frame:getParent().args
local args = frame.args
if frame.args[1] then
args = frame.args
end
return p.warn(args[1], args[2])
return p.warn(args[1], args[2])
end
end
Line 37: Line 31:
return string.format("[[%s]]", category or CATEGORY_ERRORS)
return string.format("[[%s]]", category or CATEGORY_ERRORS)
end
end
p.Templates = {
Error = {
purpose = "This template displays an error to indicate that a template is being used incorrectly. The error is displayed to readers, unlike [[Template:Warn]].",
params = {
[1] = {
name = "message",
type = "string",
desc = "The error message to display. The message will be displayed both in the article itself and as a [[Template:Warn|warning]] in page edit previews.",
default = DEFAULT_ERROR_MESSAGE,
},
[2] = {
name = "category",
type = "string",
desc = "The name of a category to include with the error so that a list of errors can be tracked. This category should be [[:Category:Hidden categories|hidden]].",
default = CATEGORY_ERRORS,
},
},
examples = {
{"This is an error"},
{"Invalid weapon type", "Category:{{Invalid Arguments}}"},
{},
}
},
Warn = {
purpose = "This template issues a warning to indicate that a template is being used incorrectly. Unlike [[Template:Error]], no error is displayed to readers; the error message is only visible to editors in page previews.",
params = {
[1] = {
name = "message",
required = true,
type = "string",
desc = "The error message to display in page edit previews.",
},
[2] = {
name = "category",
type = "string",
desc = "The name of a category to include with the warning so that a list of warnings can be tracked. This category should be [[:Category:Hidden categories|hidden]].",
default = CATEGORY_ERRORS
},
},
examples = {
{"Invalid argument <code>foo</code>"},
{"Invalid argument <code>bar</code>", "Category:{{Invalid Arguments}}"},
{},
}
}
}


return p
return p

Revision as of 23:58, 26 September 2022

This module provides functions which templates can invoke when they receive invalid input.


local utilsError = require("Module:UtilsError")

local p = {}

local DEFAULT_ERROR_MESSAGE = "Error"
local CATEGORY_ERRORS = "Category:Pages with template errors"

function p.Error(frame)
	local args = frame.args
	return p.printError(args[1], args[2])
end

function p.Warn(frame)
	local args = frame.args
	return p.warn(args[1], args[2])
end

-- Should maybe be moved to Module:UtilsError
function p.printError(message, category)
	local errorMessage = message and ("Error: " .. message) or DEFAULT_ERROR_MESSAGE
	local errorCategory = category or CATEGORY_ERRORS
	utilsError.warn(message or DEFAULT_ERROR_MESSAGE)
	local err = mw.html.create("strong")
		:addClass("error")
		:wikitext(errorMessage)
	return string.format("[[:%s|%s]][[%s]]", errorCategory, tostring(err), errorCategory)
end

function p.warn(message, category)
	utilsError.warn(message or "")
	return string.format("[[%s]]", category or CATEGORY_ERRORS)
end

return p