Module:ErrorHandling: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Manual revert
// via Wikitext Extension for VSCode
Line 20: Line 20:
         START_TIME = os.clock() * 1000,  -- Store in milliseconds
         START_TIME = os.clock() * 1000,  -- Store in milliseconds
         ERROR_COUNT = 0,
         ERROR_COUNT = 0,
        STATUS_COUNT = 0,
         ERRORS = {},
         ERRORS = {},
        STATUSES = {},
         HAS_CRITICAL_ERROR = false
         HAS_CRITICAL_ERROR = false
     }
     }
Line 51: Line 53:
      
      
     -- Return the error context for chaining
     -- Return the error context for chaining
    return context
end
-- Add a status message to the context
-- @param context The error context
-- @param source The source of the status message (function name)
-- @param message The status message
-- @param details Optional additional details
-- @return The context (for chaining)
function ErrorHandling.addStatus(context, source, message, details)
    -- Increment the status count
    context.STATUS_COUNT = (context.STATUS_COUNT or 0) + 1
   
    -- Add the status to the list
    table.insert(context.STATUSES, {
        id = context.STATUS_COUNT,
        source = source or "unknown",
        message = message or "Unknown status",
        details = details or ""
    })
   
    -- Return the context for chaining
     return context
     return context
end
end
Line 86: Line 110:
     -- Create hidden div with minimal footprint
     -- Create hidden div with minimal footprint
     return string.format('<div style="display:none"%s></div>', attrStr)
     return string.format('<div style="display:none"%s></div>', attrStr)
end
-- Format the status context for debugging output using data attributes
-- @param context The error context
-- @return HTML string with data attributes containing status information
function ErrorHandling.formatStatusOutput(context)
    -- If no statuses, return empty string
    if not context.STATUSES or #context.STATUSES == 0 then
        return ""
    end
   
    -- Build minimal data attribute div
    local divAttributes = {
        ['data-template-status'] = "1",
        ['data-status-count'] = tostring(#context.STATUSES)
    }
   
    -- Add individual status attributes with minimal naming
    for _, stat in ipairs(context.STATUSES) do
        divAttributes['data-status-' .. stat.id .. '-source'] = stat.source
        divAttributes['data-status-' .. stat.id .. '-msg'] = stat.message
        if stat.details and stat.details ~= "" then
            divAttributes['data-status-' .. stat.id .. '-details'] = stat.details
        end
    end
   
    -- Build attribute string efficiently
    local attrStr = ""
    for k, v in pairs(divAttributes) do
        attrStr = attrStr .. ' ' .. k .. '="' .. v .. '"'
    end
   
    -- Create hidden div with minimal footprint
    return string.format('<div style="display:none"%s></div>', attrStr)
end
-- Formats and combines both error and status outputs
-- @param context The context object
-- @return A string containing HTML for both errors and statuses
function ErrorHandling.formatCombinedOutput(context)
    local errorOutput = ErrorHandling.formatOutput(context)
    local statusOutput = ErrorHandling.formatStatusOutput(context)
   
    if errorOutput ~= "" and statusOutput ~= "" then
        return errorOutput .. "\n" .. statusOutput
    end
   
    return errorOutput or statusOutput
end
end