Modül:User error
A less intimidating version of the built-in "error()" function, to help editors fix their mistakes when transcluding a template.
userError(message, ...)(function)- A less intimidating version of the built-in "error()" function, to help editors fix their mistakes when transcluding a template.
- Parameters:
- Returns: Formatted error message with categories (string)
--- A less intimidating version of the built-in "error()" function, to help
-- editors fix their mistakes when transcluding a template.
-- @script userError
-- @author [[dev:User:DarthKitty|DarthKitty]]
-- @param {string} message Error message to show
-- @param {string} ... Categories to add to the page
-- @returns {string} Formatted error message with categories
-- <nowiki>
return function (message, ...)
local checkType = require('libraryUtil').checkType
checkType('User error', 1, message, 'string')
local frame = mw.getCurrentFrame()
local templateTitle = frame:getTitle()
frame = frame:getParent()
while frame ~= nil do
local title = frame:getTitle()
if mw.title.new(title):inNamespaces(10, 828) then
templateTitle = title
end
frame = frame:getParent()
end
local errorMessage = string.format(
'Error from [[%s]]: %s. Please check the linked documentation for expected input and examples.',
templateTitle,
message
)
local element = mw.html.create('strong')
:addClass('error')
:wikitext(errorMessage)
mw.addWarning(errorMessage)
for i = 1, select('#', ...) do
local category = select(i, ...)
checkType('User error', i + 1, category, 'string')
if category ~= '' then
element:wikitext('[[Category:' .. category .. ']]')
end
end
return tostring(element)
end
-- </nowiki>