Module:languages/error


Use the second parameter of getByCode() instead of directly invoking this module.
The preferred way to generate an error message for an invalid language code is to pass the parameter name holding the language code as the parameter getByCode() rather than directly invoking this module. Code in some modules directly invokes this module, but this is generally old code that should be cleaned up.

Preferred calling conventionPreferred calling convention

local m_languages = require("Module:languages")
local lang = m_languages.getByCode(frame.args[1], 1)

Here, we call getByCode with the parameter name in the second parameter.

Old (obsolete) calling conventionOld (obsolete) calling convention

require("Module:languages/error")(lang, param, text)

Looks at a supposed language code passed through a template parameter and returns a helpful error message depending on whether the language code has a valid form (two or three lowercase basic Latin letters, two or three groups of three lowercase basic Latin letters separated by hyphens).

Add the parameter value in argument #1 and the parameter name in argument #2. For instance, if parameter 1 of the template is supposed to be a language code, this function can be called the following way:

local m_languages = require("Module:languages")
local lang = m_languages.getByCode(frame.args[1]) or require("Module:languages/error")(frame.args[1], 1)

If you would like the error message to say something other than "language code", place the phrase in argument #3.


--[=[
Throw an error for an invalid language code or script code.

`code` (required) is the bad code and can be nil or a non-string.

`param` (required) is the name of the parameter in which the code was contained. It can be a string, a number (for a
	numeric param, in which case the param will show up in the error message as an ordinal such as "first" or "second"),
	or `true` if no parameter can be clearly identified. It can also be a function of one argument (an error message) or
	two arguments (an error message and a number of stack frames to ignore, same as in the error() function itself), in
	which case the function will be called with the same error message as if `true` were passed in. (This is useful e.g.
	when parsing inline modifiers, where the parameter name may not be available or may not be so clearly identifiable,
	but an error function is often available.)

`code_desc` (optional) is text describing what the code is; by default, "language code".

`template_text` (optional) is a string specifying the template that generated the error, or a function
	to generate this string. If given, it will be displayed in the error message.
]=]

local dump = mw.dumpObject

local function internal_err(param, types, param_type)
	error(("Internal error: the parameter %s must be %s, but received %s"):format(dump(param), types, param_type))
end

return function(code, param, code_desc, template_tag)
	local is_callable = require("Module:fun").is_callable

	if param == nil then
		param = true
	end
	if not code_desc then
		code_desc = "language code"
	end

	if template_tag then
		if is_callable(template_tag) then
			template_tag = template_tag()
		else
			local template_tag_type = type(template_tag)
			if template_tag_type ~= "string" then
				internal_err("template_tag", "a function, callable table or nil", "a " .. template_tag_type)
			end
		end
		template_tag = (" (original template: %s)"):format(template_tag)
	else
		template_tag = ""
	end

	local param_is_callable = is_callable(param)
	local function err(msg)
		msg = msg .. template_tag
		if not(param_is_callable) then
			error(require("Module:string utilities").ucfirst(msg) .. ".", 3)
		end
		param(msg, 3)
	end

	if not (param == true or param_is_callable) then
		local param_type = type(param)
		if not (param_type == "string" or param_type == "number") then
			internal_err("param", "a number, string, function, callable table, true or nil", param == false and "false" or "a " .. param_type)
		end
		param = ("parameter %s "):format(dump(param))
	end
	
	if code == nil or code == "" then
		if param == true or param_is_callable then
			err(code_desc .. " is missing")
		else
			err(param .. "(" .. code_desc .. ") is missing")
		end
	else
		local code_type = type(code)
		if code_type ~= "string" then
			internal_err("code", "a string or nil", "a " .. code_type)
		end
	end
	err(("%smust be a valid %s; the value %s is not valid (see [[Wiktionary:List of languages]])"):format(
		(param == true and "" or param), code_desc, dump(code)
	))
end
Category:Language and script modules