feat(plugin): improve error handling and show better error message

This commit is contained in:
Folke Lemaitre 2024-07-23 17:24:33 +02:00
parent d5686efbd0
commit c02268ac6e
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 25 additions and 16 deletions

View File

@ -146,33 +146,43 @@ function Spec:import(spec)
local imported = 0
---@type (string|(fun():LazyPluginSpec))[]
---@type {modname: string, load: fun():(LazyPluginSpec?, string?)}[]
local modspecs = {}
if type(import) == "string" then
Util.lsmod(import, function(modname, modpath)
modspecs[#modspecs + 1] = modname
package.preload[modname] = function()
return loadfile(modpath)()
end
modspecs[#modspecs + 1] = {
modname = modname,
load = function()
local mod, err = loadfile(modpath)
if mod then
return mod()
else
return nil, err
end
end,
}
end)
table.sort(modspecs, function(a, b)
return a.modname < b.modname
end)
table.sort(modspecs)
else
modspecs = { spec.import }
modspecs = { modname = import_name, load = spec.import }
end
for _, modspec in ipairs(modspecs) do
imported = imported + 1
local modname = type(modspec) == "string" and modspec or import_name
local modname = modspec.modname
Util.track({ import = modname })
self.importing = modname
-- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil
Util.try(function()
local mod = type(modspec) == "function" and modspec() or require(modspec)
if type(mod) ~= "table" then
self.importing = nil
local mod, err = modspec.load()
if err then
self:error("Failed to load `" .. modname .. "`:\n" .. err)
elseif type(mod) ~= "table" then
return self:error(
"Invalid spec module: `"
.. modname
@ -180,18 +190,17 @@ function Spec:import(spec)
.. type(mod)
.. "` was returned instead"
)
else
self:normalize(mod)
end
self:normalize(mod)
self.importing = nil
Util.track()
end, {
msg = "Failed to load `" .. modname .. "`",
on_error = function(msg)
self:error(msg)
self.importing = nil
Util.track()
end,
})
self.importing = nil
Util.track()
end
if imported == 0 then
self:error("No specs found for module " .. spec.import)