made modload catch all errors emitted by modules

This commit is contained in:
Ponali
2025-10-20 17:38:43 +02:00
parent 8ab525b9ac
commit 668ed93491
+46 -4
View File
@@ -30,11 +30,21 @@ local function loadModule(modName)
table.remove(moduleList, table.find(moduleList, modName)) table.remove(moduleList, table.find(moduleList, modName))
return true return true
end end
if not moduleData.check() then local ready = false
local status, err = xpcall(function()
ready = moduleData.check()
end, debug.traceback)
if not status then
ready = false
log.kernel.error(
string.format("[modload: %s] Could not check if module was ready: %s", modName, tostring(err or "unknown error"))
)
end
if not ready then
log.kernel.info(string.format("[modload: %s] Module not ready - skipping", modName)) log.kernel.info(string.format("[modload: %s] Module not ready - skipping", modName))
return false return false
end end
if moduleData.dependencies then if type(moduleData.dependencies) == "table" then
for _, dependency in pairs(moduleData.dependencies) do for _, dependency in pairs(moduleData.dependencies) do
if table.find(moduleList, dependency) then if table.find(moduleList, dependency) then
loadModule(dependency) loadModule(dependency)
@@ -53,10 +63,23 @@ local function loadModule(modName)
--print(modName) --print(modName)
log.kernel.info(string.format("[modload: %s] Loading module", modName)) log.kernel.info(string.format("[modload: %s] Loading module", modName))
if moduleData.init then -- I have no idea why this would not exist, but it's a failsafe if moduleData.init then -- I have no idea why this would not exist, but it's a failsafe
local status, err = xpcall(function()
moduleData.init() moduleData.init()
end, debug.traceback)
if not status then
log.kernel.error(
string.format(
"[modload: %s] An error has occured while initiating this module: %s",
modName,
tostring(err or "unknown error")
)
)
return false
else
table.insert(modulesLoaded, modName) table.insert(modulesLoaded, modName)
table.remove(moduleList, table.find(moduleList, modName)) table.remove(moduleList, table.find(moduleList, modName))
end end
end
return true return true
end end
@@ -121,11 +144,30 @@ loadAllModules()
local function checkModules() local function checkModules()
log.kernel.info("[modload] Updating module availability.") log.kernel.info("[modload] Updating module availability.")
loadAllModules() -- load modules that haven't returned true before loadAllModules() -- load modules that haven't returned true before
-- TODO: make this function exit modules that haven't returned false before (check if this is right first) -- exit modules that haven't returned false before (check if this is right first)
for _, v in pairs(table.copy(modulesLoaded)) do for _, v in pairs(table.copy(modulesLoaded)) do
if not modules[v].check() then local ready = false
local status, err = xpcall(function()
ready = modules[v].check()
end, debug.traceback)
if not status then
ready = false
log.kernel.error(
string.format(
"[modload: %s] Could not check if module was ready: %s",
modName,
tostring(err or "unknown error")
)
)
end
if not ready then
log.kernel.info(string.format("[modload: %s] Module is no longer ready: exiting", v)) log.kernel.info(string.format("[modload: %s] Module is no longer ready: exiting", v))
local status, err = xpcall(function()
modules[v].exit() modules[v].exit()
end, debug.traceback)
if not status then
log.kernel.error(string.format("[modload: %s] Could not exit module: %s", v, tostring(err or "unknown error")))
end
table.insert(moduleList, table.remove(modulesLoaded, table.find(modulesLoaded, v))) table.insert(moduleList, table.remove(modulesLoaded, table.find(modulesLoaded, v)))
end end
end end