Skip to content

Commit 326bc75

Browse files
phanenibhagwan
authored andcommitted
fix: reuse xpcall to get full traceback
1 parent a2fe1be commit 326bc75

2 files changed

Lines changed: 24 additions & 26 deletions

File tree

lua/fzf-lua/core.lua

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -237,42 +237,40 @@ end
237237
---@param cmd string?
238238
---@param opts table
239239
---@param convert_actions boolean?
240-
---@return thread, string, table
240+
---@return thread?, string, table
241241
M.fzf_wrap = function(cmd, opts, convert_actions)
242242
opts = opts or {}
243243
M.set_header(opts)
244244
if convert_actions and type(opts.actions) == "table" then
245245
opts = M.convert_reload_actions(cmd, opts)
246246
opts = M.convert_exec_silent_actions(opts)
247247
end
248-
local _co
249-
local wrapped = coroutine.wrap(function()
248+
-- Do not strt fzf, return the stringified contents and opts onlu
249+
-- used by the "combine" picker to merge inputs
250+
if opts._start == false then return nil, cmd, opts end
251+
local _co, fn_selected
252+
coroutine.wrap(function()
250253
_co = coroutine.running()
251-
if type(opts.cb_co) == "function" then opts.cb_co(_co) end
252-
local selected, exit_code = M.fzf(cmd, opts)
253-
-- If aborted (e.g. unhide process kill), do nothing
254-
if not tonumber(exit_code) then return end
255-
-- Default fzf exit callback acts upon the selected items
256-
local fn_selected = opts.fn_selected or actions.act
257-
if not fn_selected then return end
258-
-- errors thrown here gets silenced possibly
259-
-- due to a coroutine, so catch explicitly
260-
local _, err = pcall(fn_selected, selected, opts)
254+
-- xpcall to get full traceback https://www.lua.org/pil/8.5.html
255+
local _, err = xpcall(function()
256+
if type(opts.cb_co) == "function" then opts.cb_co(_co) end
257+
local selected, exit_code = M.fzf(cmd, opts)
258+
-- If aborted (e.g. unhide process kill), do nothing
259+
if not tonumber(exit_code) then return end
260+
-- Default fzf exit callback acts upon the selected items
261+
fn_selected = opts.fn_selected or actions.act
262+
if not fn_selected then return end
263+
-- errors thrown here gets silenced possibly
264+
-- due to a coroutine, so catch explicitly
265+
fn_selected(selected, opts)
266+
end, debug.traceback)
261267
-- ignore existing swap file error, the choices dialog will still be
262268
-- displayed to user to make a selection once fzf-lua exits (#1011)
263269
if err then
264-
if err:match("Vim%(edit%):E325") then return end
265-
utils.error("fn_selected threw an error: " .. debug.traceback(_co, err, 1))
266-
end
267-
end)
268-
-- Do not strt fzf, return the stringified contents and opts onlu
269-
-- used by the "combine" picker to merge inputs
270-
if opts._start ~= false then
271-
local ok, err = pcall(wrapped)
272-
if not ok and err then
273-
error(debug.traceback(_co, err, 1))
270+
if fn_selected and err:match("Vim%(edit%):E325") then return end
271+
utils.error("fn_selected threw an error: " .. err)
274272
end
275-
end
273+
end)()
276274
return _co, cmd, opts
277275
end
278276

lua/fzf-lua/shell.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,14 @@ M.stringify = function(contents, opts, fzf_field_index)
428428
return on_write(data, cb)
429429
end
430430

431-
local ok, err = pcall(function()
431+
local ok, err = xpcall(function()
432432
if type(contents) == "table" then
433433
vim.tbl_map(function(x) on_write_nl(x) end, contents)
434434
on_finish()
435435
elseif type(contents) == "function" then
436436
contents(on_write_nl, on_write, unpack(args))
437437
end
438-
end)
438+
end, debug.traceback)
439439
if not ok and err then
440440
on_finish()
441441
error(err)

0 commit comments

Comments
 (0)