dev

This module was made as a sandbox for the User error module. This documentation is kept to prevent redlinks.


-- <nowiki>
--------------------------------------------------------------------------------
-- A less intimidating version of the built-in `error` function.
--------------------------------------------------------------------------------
local p = {}
local checkType = require('libraryUtil').checkType
local i18n = require('Dev:I18n').loadMessages('User error')

--------------------------------------------------------------------------------
-- Builds an HTML error message, optionally including tracking categories. The
-- message is automatically formatted according to the user's language, and can
-- be "caught" with the `#iferror:` parser function.
--
-- @param {string} message
--     The body of the error message.
-- @param {...string} ...
--     The titles, not including namespace prefixes, of any categories to add.
-- @returns {string}
--     The HTML error message.
--------------------------------------------------------------------------------
function p._main(message, ...)
    checkType('Dev:User error', 1, message, 'string')

    local element = mw.html.create('strong')
        :addClass('error')
        :wikitext(i18n:msg('error-wrapper', message))

    for i = 1, select('#', ...) do
        local category = select(i, ...)

        checkType('Dev:User error', i + 1, category, 'string')

        if category ~= '' then
            element:wikitext('[[Category:' .. category .. ']]')
        end
    end

    return tostring(element)
end

--------------------------------------------------------------------------------
-- Redirect calls from `p(...)` to `p.main(...)`, for backwards-compatability
-- and convenience.
--------------------------------------------------------------------------------
return setmetatable(p, {
    __call = function (self, ...)
        return self._main(...)
    end,
})