Module:Error: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
local utilsError = require("Module:UtilsError")
local p = {}
local p = {}


local DEFAULT_ERROR_MESSAGE = "Error"
local utilsError = require("Module:UtilsError")
local CATEGORY_ERRORS = "Category:Pages with template errors"


function p.Error(frame)
local DEFAULT_WARN_MESSAGE = "An error occurred"
local args = frame:getParent().args
local CATEGORY_ERRORS = "Category:"..require("Module:Constants/category/templateErrors")
if frame.args[1] then
args = frame.args
end
return p.printError(args[1], args[2])
end


function p.Warn(frame)
function p.Warn(frame)
local args = frame:getParent().args
local message, category = frame.args[1], frame.args[2]
if frame.args[1] then
args = frame.args
local errorCategory = category or CATEGORY_ERRORS
end
utilsError.warn(message or DEFAULT_WARN_MESSAGE)
return p.warn(args[1], args[2])
return "[["..errorCategory.."]]"
end
end


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


function p.warn(message, category)
function p.Documentation()
utilsError.warn(message or "")
return {
return string.format("[[%s]]", category or CATEGORY_ERRORS)
Error = {
end
desc = "<p>This function displays a bolded red error message at the site of the template usage. The error links to the template's page, which should have documentation on the error.</p><p>Use this function when a template has no sensible fallback option. A full-on error is appropriate for [[Template:HW Element]] when no element type is given, for example. Defaulting to any one element would be unwise as it may display false information to the reader. Likewise, displaying nothing at all may confuse the reader. They may think the element simply hasn't been filled yet, or that maybe the weapon has no element. Better that they know an editor made a mistake. The reader might even be motivated to become an editor and fix it themselves.</p>",
 
frameParams = {
p.Templates = {
[1] = {
Error = {
name = "message",
purpose = "This template displays an error to indicate that a template is being used incorrectly. The error is displayed to readers, unlike [[Template:Warn]].",
default = "Error",
params = {
desc = "The error message.",
[1] = {
},
name = "message",
[2] = {
type = "string",
name = "category",
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 = "Category:Pages with template errors",
default = DEFAULT_ERROR_MESSAGE,
desc = "The name of the maintenance category to output alongside the error message so that the error can be tracked.",
},
},
},
[2] = {
cases = {
name = "category",
resultOnly = true,
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]].",
desc = "Note that the error links to [[Module:Documentation]] because that's the page invoking the function in these examples. Normally a template page would be linked - see [[Template:HW Element]] for example.",
default = CATEGORY_ERRORS,
args = {"This is an error"},
},
{
args = {"Invalid weapon type" , "Category:{{Invalid Arguments}}"},
},
{
args = {},
}
},
},
},
},
examples = {
Warn = {
{"This is an error"},
desc = "<p>This function [https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.addWarning issues a warning] which only editors can see when previewing the page.</p><p>Use this function when a template has a sensible fallback in the face of an error and the error needn't be displayed to the user. For example, a template like [[Template:Color]] needn't display an error to readers if given an invalid color name. In this case, the text can still be shown without color as a fallback. The template can quietly inform editors without affecting readers.</p>",
{"Invalid weapon type", "Category:{{Invalid Arguments}}"},
frameParams = {
{},
[1] = {
}
name = "message",
},
desc = "The warning message.",
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.",
[2] = {
params = {
name = "category",
[1] = {
default = "Category:Pages with template errors",
name = "message",
desc = "The name of the maintenance category to output so that the warning can be tracked.",
required = true,
},
type = "string",
desc = "The error message to display in page edit previews.",
},
},
[2] = {
cases = {
name = "category",
resultOnly = true,
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]].",
args = {"Invalid argument <code>foo</code>"},
default = CATEGORY_ERRORS
},
{
args = {"Invalid argument <code>bar</code>|Category:{{Invalid Arguments}}"},
},
{
args = {}
},
},
},
},
},
examples = {
{"Invalid argument <code>foo</code>"},
{"Invalid argument <code>bar</code>", "Category:{{Invalid Arguments}}"},
{},
}
}
}
}
end


return p
return p

Latest revision as of 18:16, 27 October 2022

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

This module exports the following functions.

Warn

{{#invoke:Error|Warn|<message>|<category>}}

This function issues a warning which only editors can see when previewing the page.

Use this function when a template has a sensible fallback in the face of an error and the error needn't be displayed to the user. For example, a template like Template:Color needn't display an error to readers if given an invalid color name. In this case, the text can still be shown without color as a fallback. The template can quietly inform editors without affecting readers.

Parameters

ParameterStatusDescriptionDefault value
1messageoptionalThe warning message.
2categoryoptionalThe name of the maintenance category to output so that the warning can be tracked.Category:Pages with template errors

Examples

#InputCategories added
1
{{#invoke:Error|Warn|Invalid argument <code>foo</code>}}
2
{{#invoke:Error|Warn|Invalid argument <code>bar</code>|Category:{{Invalid Arguments}}}}
3
{{#invoke:Error|Warn}}

Error

{{#invoke:Error|Error|<message>|<category>}}

This function displays a bolded red error message at the site of the template usage. The error links to the template's page, which should have documentation on the error.

Use this function when a template has no sensible fallback option. A full-on error is appropriate for Template:HW Element when no element type is given, for example. Defaulting to any one element would be unwise as it may display false information to the reader. Likewise, displaying nothing at all may confuse the reader. They may think the element simply hasn't been filled yet, or that maybe the weapon has no element. Better that they know an editor made a mistake. The reader might even be motivated to become an editor and fix it themselves.

Parameters

ParameterStatusDescriptionDefault value
1messageoptionalThe error message.Error
2categoryoptionalThe name of the maintenance category to output alongside the error message so that the error can be tracked.Category:Pages with template errors

Examples

#InputResultCategories added
Note that the error links to Module:Documentation because that's the page invoking the function in these examples. Normally a template page would be linked - see Template:HW Element for example.
4
{{#invoke:Error|Error|This is an error}}
Error: This is an error
5
{{#invoke:Error|Error|Invalid weapon type|Category:{{Invalid Arguments}}}}
Error: Invalid weapon type
6
{{#invoke:Error|Error}}
Error

local p = {}

local utilsError = require("Module:UtilsError")

local DEFAULT_WARN_MESSAGE = "An error occurred"
local CATEGORY_ERRORS = "Category:"..require("Module:Constants/category/templateErrors")

function p.Warn(frame)
	local message, category = frame.args[1], frame.args[2]
	
	local errorCategory = category or CATEGORY_ERRORS
	utilsError.warn(message or DEFAULT_WARN_MESSAGE)
	return "[["..errorCategory.."]]"
end

function p.Error(frame)
	local message, category = frame.args[1], frame.args[2]
	
	local errorCategory = category or CATEGORY_ERRORS
	return utilsError.error(message, true).."[["..errorCategory.."]]"
end

function p.Documentation()
	return {
		Error = {
			desc = "<p>This function displays a bolded red error message at the site of the template usage. The error links to the template's page, which should have documentation on the error.</p><p>Use this function when a template has no sensible fallback option. A full-on error is appropriate for [[Template:HW Element]] when no element type is given, for example. Defaulting to any one element would be unwise as it may display false information to the reader. Likewise, displaying nothing at all may confuse the reader. They may think the element simply hasn't been filled yet, or that maybe the weapon has no element. Better that they know an editor made a mistake. The reader might even be motivated to become an editor and fix it themselves.</p>",
			frameParams = {
				[1] = {
					name = "message",
					default = "Error",
					desc = "The error message.",
				},
				[2] = {
					name = "category",
					default = "Category:Pages with template errors",
					desc = "The name of the maintenance category to output alongside the error message so that the error can be tracked.",
				},
			},
			cases = {
				resultOnly = true,
				{
					desc = "Note that the error links to [[Module:Documentation]] because that's the page invoking the function in these examples. Normally a template page would be linked - see [[Template:HW Element]] for example.",
					args = {"This is an error"},
				},
				{
					args = 	{"Invalid weapon type" , "Category:{{Invalid Arguments}}"},
				},
				{
					args = {},
				}
			},
		},
		Warn = {
			desc = "<p>This function [https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.addWarning issues a warning] which only editors can see when previewing the page.</p><p>Use this function when a template has a sensible fallback in the face of an error and the error needn't be displayed to the user. For example, a template like [[Template:Color]] needn't display an error to readers if given an invalid color name. In this case, the text can still be shown without color as a fallback. The template can quietly inform editors without affecting readers.</p>",
			frameParams = {
				[1] = {
					name = "message",
					desc = "The warning message.",
				},
				[2] = {
					name = "category",
					default = "Category:Pages with template errors",
					desc = "The name of the maintenance category to output so that the warning can be tracked.",
				},
			},
			cases = {
				resultOnly = true,
				{
					args = {"Invalid argument <code>foo</code>"},
				},
				{
					args = {"Invalid argument <code>bar</code>|Category:{{Invalid Arguments}}"},
				},
				{
					args = {}
				},
			},
		},
	}
end

return p