Skip to content

Commit

Permalink
feat: wrap pcall in try util method to show error on error
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Sep 10, 2022
1 parent 1097809 commit c6f9621
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lua/settings/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local M = {}

function M.setup(opts)
require("settings.config").setup(opts)
require("settings.commands").setup()
require("settings.plugins").setup()
require("settings.util").try(function()
require("settings.config").setup(opts)
require("settings.commands").setup()
require("settings.plugins").setup()
end)
end

---@generic T : table
Expand Down
24 changes: 19 additions & 5 deletions lua/settings/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,28 @@ end

function M.protect(fn, msg)
return function(...)
local ret = table.pack(pcall(fn, ...))
if not ret[1] then
M.error((msg and msg .. "\n" or "") .. ret[2])
end
return table.unpack(ret)
local args = table.pack(...)

return xpcall(function()
return fn(table.unpack(args))
end, function(err)
local lines = {}
if msg then
table.insert(lines, msg)
end
table.insert(lines, err)
table.insert(lines, debug.traceback("", 3))

M.error(table.concat(lines, "\n"))
return err
end)
end
end

function M.try(fn, msg)
M.protect(fn, msg)()
end

function M.config_path()
return vim.loop.fs_realpath(vim.fn.stdpath("config"))
end
Expand Down

0 comments on commit c6f9621

Please sign in to comment.