Открыть меню
Открыть персональное меню
Вы не представились системе
Your IP address will be publicly visible if you make any edits.

Модуль:Error/Ext

Материал из wiki.iccup.org

Для документации этого модуля может быть создана страница Модуль:Error/Ext/doc

local Array = require('Module:Array')
local Error = require('Module:Error')
local Json = require('Module:Json')
local PageVariableNamespace = require('Module:PageVariableNamespace')
local Table = require('Module:Table')

local pageVars = PageVariableNamespace('ErrorStash')

local _local_errors = {}

local ErrorExt = {}

---@param error Error
function ErrorExt.logAndStash(error)
	ErrorExt.Stash.add(error)
	ErrorExt.log(error)
end

---@param error Error
function ErrorExt.log(error)
	mw.log(ErrorExt.makeFullDetails(error))
	mw.log()
end

---@param tbl table
---@return table
---@overload fun(tbl: any): {}
local function tableOrEmpty(tbl)
	return type(tbl) == 'table' and tbl or {}
end

---@param error Error
---@return string
function ErrorExt.makeFullDetails(error)
    -- Проверяем, что каждый аргумент является таблицей, если нет - преобразуем в таблицу
    local parts = Array.extend(
        type(error.header) == "string" and {error.header} or {},
        type(error.message) == "string" and {error.message} or {},
        type(ErrorExt.makeFullStackTrace(error)) == "string" and {ErrorExt.makeFullStackTrace(error)} or {},
        type(ErrorExt.printExtraProps(error)) == "string" and {ErrorExt.printExtraProps(error)} or {}
    )
    
    return table.concat(parts, '\n')
end


---Builds a string for fields not covered by the other functions in this module.
---Returns nil if there are no extra fields.
---@param error Error
---@return string?
function ErrorExt.printExtraProps(error)
	local extraProps = Table.copy(error)
	extraProps.message = nil
	extraProps.header = nil
	extraProps.stacks = nil
	extraProps.originalErrors = nil
	if type(extraProps.childErrors) == 'table' then
		extraProps.childErrors = Array.map(extraProps.childErrors, ErrorExt.makeFullDetails)
	end

	if Table.isNotEmpty(extraProps) then
		return 'Additional properties: \n' .. mw.dumpObject(extraProps)
	else
		return nil
	end
end

---@param error Error
---@return string
function ErrorExt.makeFullStackTrace(error)
    -- Проверяем, что error.stacks является таблицей, иначе задаём пустую таблицу
    local stacks = type(error.stacks) == 'table' and error.stacks or {}

    local parts = Array.extend(
        stacks, -- Теперь это точно таблица
        Array.flatten(Array.map(tableOrEmpty(error.originalErrors), function(originalError)
            return {
                '',
                'Error was thrown while handling:',
                originalError.message,
                ErrorExt.makeFullStackTrace(originalError),
            }
        end))
    )

    return table.concat(parts, '\n')
end



local Stash = {}
ErrorExt.Stash = Stash

---Adds an Error instance to the local store.
---@param error Error
---@param storeAsPageVar boolean?
function Stash.add(error, storeAsPageVar)
	if storeAsPageVar then
		local count = tonumber(pageVars:get('count')) or 0
		pageVars:set('count', count + 1)
		pageVars:set(count + 1, Json.stringify{error})
	else
		table.insert(_local_errors, error)
	end
end

---Returns all errors (locally and from page variables), and clears the store.
---@return Error[]
function Stash.retrieve()
	local errors = {}

	local count = tonumber(pageVars:get('count')) or 0
	pageVars:delete('count')
	for i = 1, count do
		-- Используем Array.extend вместо Array.extendWith
		Array.extend(errors, Array.map(Json.parse(pageVars:get(i)), Error))
		pageVars:delete(i)
	end

	-- Используем Array.extend вместо Array.extendWith
	Array.extend(errors, _local_errors)
	_local_errors = {}

	return errors
end


return ErrorExt