mirror of https://github.com/folke/lazy.nvim.git
feat(plugin): improve error handling and show better error message
This commit is contained in:
parent
d5686efbd0
commit
c02268ac6e
|
@ -146,33 +146,43 @@ function Spec:import(spec)
|
||||||
|
|
||||||
local imported = 0
|
local imported = 0
|
||||||
|
|
||||||
---@type (string|(fun():LazyPluginSpec))[]
|
---@type {modname: string, load: fun():(LazyPluginSpec?, string?)}[]
|
||||||
local modspecs = {}
|
local modspecs = {}
|
||||||
|
|
||||||
if type(import) == "string" then
|
if type(import) == "string" then
|
||||||
Util.lsmod(import, function(modname, modpath)
|
Util.lsmod(import, function(modname, modpath)
|
||||||
modspecs[#modspecs + 1] = modname
|
modspecs[#modspecs + 1] = {
|
||||||
package.preload[modname] = function()
|
modname = modname,
|
||||||
return loadfile(modpath)()
|
load = function()
|
||||||
end
|
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)
|
end)
|
||||||
table.sort(modspecs)
|
|
||||||
else
|
else
|
||||||
modspecs = { spec.import }
|
modspecs = { modname = import_name, load = spec.import }
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, modspec in ipairs(modspecs) do
|
for _, modspec in ipairs(modspecs) do
|
||||||
imported = imported + 1
|
imported = imported + 1
|
||||||
local modname = type(modspec) == "string" and modspec or import_name
|
local modname = modspec.modname
|
||||||
Util.track({ import = modname })
|
Util.track({ import = modname })
|
||||||
self.importing = modname
|
self.importing = modname
|
||||||
-- unload the module so we get a clean slate
|
-- unload the module so we get a clean slate
|
||||||
---@diagnostic disable-next-line: no-unknown
|
---@diagnostic disable-next-line: no-unknown
|
||||||
package.loaded[modname] = nil
|
package.loaded[modname] = nil
|
||||||
Util.try(function()
|
Util.try(function()
|
||||||
local mod = type(modspec) == "function" and modspec() or require(modspec)
|
local mod, err = modspec.load()
|
||||||
if type(mod) ~= "table" then
|
if err then
|
||||||
self.importing = nil
|
self:error("Failed to load `" .. modname .. "`:\n" .. err)
|
||||||
|
elseif type(mod) ~= "table" then
|
||||||
return self:error(
|
return self:error(
|
||||||
"Invalid spec module: `"
|
"Invalid spec module: `"
|
||||||
.. modname
|
.. modname
|
||||||
|
@ -180,18 +190,17 @@ function Spec:import(spec)
|
||||||
.. type(mod)
|
.. type(mod)
|
||||||
.. "` was returned instead"
|
.. "` was returned instead"
|
||||||
)
|
)
|
||||||
|
else
|
||||||
|
self:normalize(mod)
|
||||||
end
|
end
|
||||||
self:normalize(mod)
|
|
||||||
self.importing = nil
|
|
||||||
Util.track()
|
|
||||||
end, {
|
end, {
|
||||||
msg = "Failed to load `" .. modname .. "`",
|
msg = "Failed to load `" .. modname .. "`",
|
||||||
on_error = function(msg)
|
on_error = function(msg)
|
||||||
self:error(msg)
|
self:error(msg)
|
||||||
self.importing = nil
|
|
||||||
Util.track()
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
self.importing = nil
|
||||||
|
Util.track()
|
||||||
end
|
end
|
||||||
if imported == 0 then
|
if imported == 0 then
|
||||||
self:error("No specs found for module " .. spec.import)
|
self:error("No specs found for module " .. spec.import)
|
||||||
|
|
Loading…
Reference in New Issue