Fix call some methods more safely

This commit is contained in:
2023-09-19 15:56:39 +02:00
parent e58c281617
commit 41bcb60bdc
5 changed files with 112 additions and 33 deletions
+16 -1
View File
@@ -52,7 +52,8 @@ end
--- @param exes table: Array of exes
function M.assert_any_installed(exes)
if not M.any_installed(exes) then
error("At least one of the following is required:\n" .. table.concat(exes, ", "))
error("At least one of the following is required:\n" ..
table.concat(exes, ", "))
end
end
@@ -98,4 +99,18 @@ function M.err(msg, title)
M.notify(msg, title, vim.log.levels.ERROR)
end
function M.try_require(module, err_title, on_success)
local has_module, resp = pcall(require, module)
if has_module then
if not on_success then
return
end
return on_success(resp)
end
M.err(("Failed to load module %s"):format(module), err_title)
end
return M