Module:UtilsError

local p = {} local h = {}

local i18n = require("Module:I18n") local s = i18n.getString local utilsTable = require("Module:UtilsTable")

function code(s) return string.format(" ", s) end

function p.warn(msg, level) local callStack = debug.traceback("", 2 + (level or 0)) local output = msg .. "\n\n" for line in string.gmatch(callStack, "\tModule:[^:]*:[^\n]*\n") do		line = string.gsub(line, "\tModule:([^:]*):(.*)", ":::Module:%1:%2") output = output .. line .. "\n" end mw.addWarning(output) end

function p.required(...) local msg = s("msg.required", {		path = h.path(...)	}) p.warn(msg, 1) return s("cat.invalidFunctionArguments"), msg end

function p.type(expectedType, actualType, ...) local msg = s("msg.type", {		path = h.path(...), 		expectedType = code(expectedType), 		actualType = code(actualType)	}) p.warn(msg, 1) return s("cat.invalidFunctionArguments"), msg end

function p.empty(value, ...) local msg = s("msg.empty", {		path = h.path(...),		actualValue = code(utilsTable.print(value))	}) p.warn(msg, 1) return s("cat.invalidFunctionArguments"), msg end

function p.enum(enum, value, ...) local msg = s("msg.enum", {		path = h.path(...),		actualValue = code(value),		enumDetails = h.enumDetails(enum)	}) p.warn(msg, 1) return s("cat.invalidFunctionArguments"), msg end

function p.enumKey(enum, key, ...) local msg = s("msg.enumKey", {		key = code(key), 		path = h.path(...),		enumDetails = h.enumDetails(enum)	}) p.warn(msg, 1) return s("cat.invalidFunctionArguments"), msg end

function h.enumDetails(enum) if type(enum) == "table" then return s("msg.enumAccepted", {			values = code(utilsTable.print(enum))		}) elseif type(enum) == "string" then return s("msg.enumReference", {			referencePage = enum		}) end return "" end

function h.path(...) local pathComponents = {...} local path = pathComponents[1] for _, pathComponent in pairs(utilsTable.tail(pathComponents)) do path = path .. "[" .. utilsTable.print(pathComponent) .. "]"	end return code(path) end

i18n.loadStrings({	en = {		msg = {			empty = "${path} must be non-empty but is ${actualValue}.",			required = "${path} is required but is .",			type = "${path} is type ${actualType} but type ${expectedType} was expected.",			enum = "${path} has unexpected value ${actualValue}. ${enumDetails}",			enumKey = "${key} is not an acceptable key for for ${path}. ${enumDetails}",			enumAccepted = "The accepted values are: ${values}",			enumReference = "For a list of accepted values, refer to ${referencePage}.",		},		cat = {			invalidFunctionArguments = "Category:Pages with invalid function arguments",		},	} })

p.Documentation = { {		name = "warn", params = { {				name = "msg", description = "Warning message to log above the edit preview area", },			{				name = "level", description = "The number of additional call frames to skim off the top of the stack trace", }		},		returns = "Returns nil, but logs the above message along with a stack trace (of only the relevant call frames). Preview this page for an example of output." },	{		name = "required", params = { {				name = "...", description = "Strings indicating the path to a value that is required but is nil." }		},		returns = { "The name of the error category to print.", "The warning message that is logged using .", },		cases = { {				args = { "args", 1, "foo" }, expected = { s("cat.invalidFunctionArguments"), ' is required but is  .', }			}		}	},	{		name = "empty", params = { {				name = "value", description = An empty value that shouldn't be. Either,  , or },			{				name = "...", description = "Strings indicating the path to the value that is empty but was expected not to be." }		},		returns = { "The name of the error category to print.", "The warning message that was logged using .", },		cases = { {				args = { {}, "args", "foo" }, expected = { s("cat.invalidFunctionArguments"), ' must be non-empty but is  .', }			}		}	},	{		name = "type", params = { {				name = "...", description = "Strings indicating the path to a value that is not the expected type." }		},		returns = { "The name of the error category to print.", "The warning message that was logged using .", },		cases = { {				args = { "number", "string", "args", 1 }, expected = { s("cat.invalidFunctionArguments"), ' is type   but type   was expected.', }			}		}	},	{		name = "enum", params = { {				name = "enum", description = "An array of values that a certain variable is expected to have. Or, a string holding a link to a reference page with more information about what the right values are.", },			{				name = "value", description = "A value that does not belong to the enum as expected." },			{				name = "...", description = "Strings indicating the path to the value in question.", }		},		returns = { "The name of the error category to print.", "The warning message that was logged using .", },		cases = { {				args = { {"Kooloo", "Limpah"}, "Alakazam", "args", "magicWords" }, expected = { s("cat.invalidFunctionArguments"), has unexpected value . The accepted values are:  , }			},			{				args = { "Template:Color", "ZA Purple", "args", "color" }, expected = { s("cat.invalidFunctionArguments"), ' has unexpected value. For a list of accepted values, refer to Template:Color.', }			}		}	},	{		name = "enumKey", params = { {				name = "enum", description = "An array that indicate what keys a table can have. Or, a string holding a link to a reference page with more information about what the right values are.", },			{				name = "key", description = "A key that is not in the above array", },			{				name = "...", description = "One or more strings indicating the path to the table with the key in question", },		},		returns = { "The name of the error category to print.", "The warning message that was logged using .", },		cases = { {				args = { {"foo", "bar", "baz"}, "quux", "args" }, expected = { s("cat.invalidFunctionArguments"), ' is not an acceptable key for for. The accepted values are: ', }			},			{				args = { "Module:UtilsGame", "💩", "args", "games" }, expected = { s("cat.invalidFunctionArguments"), ' is not an acceptable key for for. For a list of accepted values, refer to Module:UtilsGame.', }			}		}	} }

return p