July 25, 2020

👭 Knight Challenge #11 👬

Want to try your hand at these challenges? There's a couple of things you can do!
From writing, to research, to images, find your preferred way to contribute with our eleventh theme: Couples!

Latest Announcements

Module:UtilsError

From Zelda Wiki, the Zelda encyclopedia
Jump to: navigation, search

A utility module for error-handling, mainly with respect to data validation.

Contents

warn

warn(msg, [options])

Parameters
  • msg
    Warning message to log above the edit preview area
  • [options]
    [traceBack=true]
    If true, prints a stack trace of all module frames. If the invoking frame is a template, the name of the template is appended to the stack trace.
    [omitFrames=0]
    Number of additional frames to omit from the top of the stack trace.
Returns
  • Returns nil, but logs the above message, possibly with a stack trace. Preview this page for an example of output.
Examples
InputOutputStatus
warn("I AM ERROR")
nil
Green check.svg
warn("I AM ERROR", { omitFrames = 2 })
nil
Green check.svg
warn("I AM ERROR", { traceBack = false })
nil
Green check.svg

local p = {}

function p.warn(msg, options)
	local options = options or {}
	local omitFrames = options.omitFrames or 0
	local traceBack = options.traceBack
	
	omitFrames = omitFrames or 0
	local errorSource
	local invokeFrame = mw.getCurrentFrame():getParent():getTitle()
	if mw.title.new(invokeFrame).nsText == "Template" then
		local template ="[[" .. mw.getCurrentFrame():getParent():getTitle() .. "]]"
		msg = "Misuse of " .. template .. ": " .. msg
		traceBack = false
	end
	if traceBack ~= false then
		msg = msg .. "\n"
		local callStack = debug.traceback("", 2)
		msg = msg .. "\n"
		for line in string.gmatch(callStack, "\tModule:[^:]*:[^\n]*\n") do
			if omitFrames ~= 0 then
				omitFrames = omitFrames - 1
			else
				line = string.gsub(line, "\tModule:([^:]*):(.*)", '::<span class="hidden-link">[[Module:%1]]:%2</span>') 
				msg = msg .. line .. "\n"
			end
		end
	end
	mw.addWarning(msg)
end

p.Schemas = {
	warn = {
		msg = {
			type  = "string",
			required = true,
			desc = "Warning message to log above the edit preview area",
		},
		options = {
			type = "record",
			properties = {
				{
					name = "traceBack",
					type = "boolean",
					default = true,
					desc = "If true, prints a stack trace of all module frames. If the invoking frame is a template, the name of the template is appended to the stack trace.",
				},
				{
					name = "omitFrames",
					type = "number",
					default = 0,
					desc = "Number of additional frames to omit from the top of the stack trace."
				},
			}
		}
	}
}

p.Documentation = {
	warn = {
		params = {"msg", "options"},
		returns = "Returns nil, but logs the above message, possibly with a stack trace. Preview this page for an example of output.",
		cases = {
			{
				args = {"I AM ERROR"},
			},
			{
				args = {"I AM ERROR", { omitFrames = 2 }},
			},
			{
				args = {"I AM ERROR", { traceBack = false }},
			}
		},
	},
}

return p