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(name, path) local msg = s("msg.required", {		path = h.path(name, path)	}) p.warn(msg, 1) return msg end

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

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

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

function p.enumKey(enum, key, name, path) local msg = s("msg.enumKey", {		key = code(key), 		path = h.path(name, path),		enumDetails = h.enumDetails(enum)	}) p.warn(msg, 1) return 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(name, pathComponents) local path = name for _, pathComponent in pairs(pathComponents or {}) 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}.",		}	} })

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", "[path]" }, returns = "The warning message that was logged using .", cases = { {				args = { "args", {1, "foo"} }, expected = ' is required but is  .', }		}	},	{		name = "empty", params = { "value", "name", "[path]" }, returns = "The warning message that was logged using .", cases = { {				args = { {}, "args", {"foo"} }, expected = ' must be non-empty but is  .', }		}	},	{		name = "type", params = { "expectedType", "actualType", "name", "[path]" }, returns = "The warning message that was logged using .", cases = { {				args = { "number", "string", "args", {1} }, expected = ' is type   but type   was expected.', }		}	},	{		name = "enum", params = { "enum", "value", "name", "[path]" }, returns = "The warning message that was logged using .", cases = { {				args = { {"Kooloo", "Limpah"}, "Alakazam", "magicWords" }, expected =  has unexpected value  . The accepted values are:  , },			{				args = { "Template:Color", "ZA Purple", "color" }, expected = ' has unexpected value. For a list of accepted values, refer to Template:Color.', }		}	},	{		name = "enumKey", params = { "enum", "key", "[path]" }, returns = "The warning message that was logged using .", cases = { {				args = { {"foo", "bar", "baz"}, "quux", "args" }, expected = ' is not an acceptable key for for. The accepted values are: ', },			{				args = { "Module:UtilsGame", "💩", "args", {"games"} }, expected = ' is not an acceptable key for for. For a list of accepted values, refer to Module:UtilsGame.', }		}	} }

return p