Skip to content

Commit 4c7b539

Browse files
fix(cron): Handle errors in cron and send system notification about error
1 parent 79e2c20 commit 4c7b539

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

lua/orgmode/files/headline.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ function Headline:get_todo()
304304
local keywords_info = todo_keywords.KEYS
305305

306306
-- A valid keyword can only be the first child
307-
local todo_node = self:_get_child_node('item'):named_child(0)
307+
local first_item_node = self:_get_child_node('item')
308+
local todo_node = first_item_node and first_item_node:named_child(0)
308309
if not todo_node then
309310
return nil, nil, nil
310311
end

lua/orgmode/init.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,23 @@ function Org.action(cmd, opts)
208208
end
209209

210210
function Org.cron(opts)
211-
local config = require('orgmode.config'):extend(opts or {})
212-
if not config.notifications.cron_enabled then
213-
return vim.cmd([[qa!]])
214-
end
215-
Org.files:load():next(vim.schedule_wrap(function()
216-
---@diagnostic disable-next-line: inject-field
211+
local ok, result = pcall(function()
212+
local config = require('orgmode.config'):extend(opts or {})
213+
if not config.notifications.cron_enabled then
214+
return vim.cmd([[qa!]])
215+
end
216+
Org.files:load_sync(true, 20000)
217217
instance.notifications = require('orgmode.notifications')
218218
:new({
219219
files = Org.files,
220220
})
221221
:cron()
222-
end))
222+
end)
223+
224+
if not ok then
225+
require('orgmode.utils').system_notification('Orgmode failed to run cron: ' .. tostring(result))
226+
return vim.cmd([[qa!]])
227+
end
223228
end
224229

225230
function Org.instance()

lua/orgmode/utils/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ function utils.writefile(file, data)
7979
end)
8080
end
8181

82+
function utils.system_notification(message)
83+
if vim.fn.executable('notify-send') == 1 then
84+
vim.loop.spawn('notify-send', { args = { message } })
85+
end
86+
87+
if vim.fn.executable('terminal-notifier') == 1 then
88+
vim.loop.spawn('terminal-notifier', { args = { '-message', message } })
89+
end
90+
end
91+
8292
function utils.open(target)
8393
if vim.fn.executable('xdg-open') == 1 then
8494
return vim.fn.system(string.format('xdg-open %s', target))

lua/orgmode/utils/promise.lua

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,31 @@ end
264264
--- @return any
265265
function Promise.wait(self, timeout)
266266
local is_done = false
267+
local has_error = false
267268
local result = nil
268269

269-
self:next(function(...)
270-
result = PackedValue.new(...)
271-
is_done = true
272-
end)
270+
self
271+
:next(function(...)
272+
result = PackedValue.new(...)
273+
is_done = true
274+
end)
275+
:catch(function(...)
276+
has_error = true
277+
result = PackedValue.new(...)
278+
is_done = true
279+
end)
273280

274281
vim.wait(timeout or 5000, function()
275282
return is_done
276283
end, 1)
277284

278-
return result and result:unpack()
285+
local value = result and result:unpack()
286+
287+
if has_error then
288+
return error(value)
289+
end
290+
291+
return value
279292
end
280293

281294
--- Equivalents to JavaScript's Promise.all.

0 commit comments

Comments
 (0)