Skip to content

Commit 02a0a66

Browse files
committed
fix(event_manager): log errors in callbacks
We were swallowing some errors (e.g. vim.filetype.match). None of them seemed important but it's possible there will be errors in the future that we need to surface. Added util.pcall_trace that will return a full stacktrace in result, rather than just the error.
1 parent 13a6efe commit 02a0a66

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

lua/opencode/event_manager.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local state = require('opencode.state')
22
local config = require('opencode.config')
33
local ThrottlingEmitter = require('opencode.throttling_emitter')
4+
local util = require('opencode.util')
45

56
--- @class EventInstallationUpdated
67
--- @field type "installation.updated"
@@ -278,12 +279,16 @@ function EventManager:emit(event_name, data)
278279

279280
local event = { type = event_name, properties = data }
280281

281-
if require('opencode.config').debug.capture_streamed_events then
282+
if config.debug.capture_streamed_events then
282283
table.insert(self.captured_events, vim.deepcopy(event))
283284
end
284285

285286
for _, callback in ipairs(listeners) do
286-
pcall(callback, data)
287+
local ok, result = util.pcall_trace(callback, data)
288+
289+
if not ok then
290+
vim.notify('Error calling ' .. event_name .. ' listener: ' .. result, vim.log.levels.ERROR)
291+
end
287292
end
288293
end
289294

lua/opencode/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125

126126
---@class OpencodeDebugConfig
127127
---@field enabled boolean
128+
---@field capture_streamed_events any[]
128129

129130
--- @class OpencodeProviders
130131
--- @field [string] string[]

lua/opencode/util.lua

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,13 @@ function M.format_time(timestamp)
198198
if timestamp > 1e12 then
199199
timestamp = math.floor(timestamp / 1000)
200200
end
201-
201+
202202
local now = os.time()
203-
local today_start = os.time(os.date('*t', now)) - os.date('*t', now).hour * 3600 - os.date('*t', now).min * 60 - os.date('*t', now).sec
204-
203+
local today_start = os.time(os.date('*t', now))
204+
- os.date('*t', now).hour * 3600
205+
- os.date('*t', now).min * 60
206+
- os.date('*t', now).sec
207+
205208
if timestamp >= today_start then
206209
return os.date('%I:%M %p', timestamp)
207210
else
@@ -416,6 +419,10 @@ end
416419
--- @param filename string filename, possibly including path
417420
--- @return string markdown_filetype
418421
function M.get_markdown_filetype(filename)
422+
if not filename or filename == '' then
423+
return ''
424+
end
425+
419426
local file_type_overrides = {
420427
javascriptreact = 'jsx',
421428
typescriptreact = 'tsx',
@@ -476,4 +483,11 @@ function M.parse_run_args(args)
476483
return opts, prompt
477484
end
478485

486+
---pcall but returns a full stacktrace on error
487+
function M.pcall_trace(fn, ...)
488+
return xpcall(fn, function(err)
489+
return debug.traceback(err, 2)
490+
end, ...)
491+
end
492+
479493
return M

0 commit comments

Comments
 (0)