Skip to content

Commit 9851bd3

Browse files
committed
feat(provider): more obvious, helpful errors when unavailable
#68
1 parent 2361ca5 commit 9851bd3

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

lua/opencode/api/command.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ function M.command(command, callback)
3131
-- No need to register SSE here - commands don't trigger any.
3232
-- (except maybe the `input_*` commands? but no reason for user to use those).
3333

34-
require("opencode.provider").show()
34+
-- Swallow errors - more of a preference than a requirement here
35+
pcall(require("opencode.provider").show)
3536

3637
require("opencode.cli.client").tui_execute_command(command, port, callback)
3738
end)

lua/opencode/api/prompt.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function M.prompt(prompt, opts, callback)
3333
return
3434
end
3535

36-
require("opencode.provider").show()
36+
-- Swallow errors - more of a preference than a requirement here
37+
pcall(require("opencode.provider").show)
3738

3839
require("opencode.util").chain({
3940
function(next)

lua/opencode/cli/server.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ function M.get_port(callback)
174174
end
175175
end,
176176
function(next)
177-
require("opencode.provider").start()
177+
local ok, result = pcall(require("opencode.provider").start)
178+
if not ok then
179+
vim.notify("Failed to start `opencode`: " .. result, vim.log.levels.ERROR)
180+
end
178181
-- Always proceed - even if `opencode` wasn't started, failing to find it will give a more helpful error message.
179182
next()
180183
end,

lua/opencode/health.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
local M = {}
22

3-
local external_instructions =
4-
"Or launch `opencode` with your own method and optionally override `vim.g.opencode_opts.provider` for convenience, then use `opencode.nvim` normally."
5-
63
function M.check()
74
vim.health.start("opencode.nvim")
85

@@ -86,16 +83,18 @@ function M.check()
8683
if snacks.picker and snacks.config.get("terminal", {}).enabled then
8784
vim.health.ok("`snacks.terminal` is enabled: will default to `snacks` provider.")
8885
else
89-
vim.health.warn("`snacks.terminal` is disabled: the `snacks` provider will not be available.", {
86+
vim.health.warn("`snacks.terminal` is disabled: the default `snacks` provider will not be available.", {
9087
"Enable `snacks.terminal`",
91-
external_instructions,
88+
"Or launch and manage `opencode` yourself",
89+
"Or configure `vim.g.opencode_opts.provider`",
9290
})
9391
end
9492
else
9593
vim.health.warn("`snacks.nvim` is not available: `ask()` and `select()` will not be enhanced.")
96-
vim.health.warn("`snacks.nvim` is not available: the `snacks` provider will not be available.", {
94+
vim.health.warn("`snacks.nvim` is not available: the default `snacks` provider will not be available.", {
9795
"Install `snacks.nvim`",
98-
external_instructions,
96+
"Or launch and manage `opencode` yourself",
97+
"Or configure `vim.g.opencode_opts.provider`",
9998
})
10099
end
101100
end

lua/opencode/provider.lua

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ function M.toggle()
6060
provider:toggle()
6161
started = true
6262
else
63-
vim.notify(
64-
"No `provider.toggle` available — configure `vim.g.opencode_opts.provider`, or install `snacks.nvim` to use the default provider",
65-
vim.log.levels.ERROR,
66-
{ title = "opencode" }
67-
)
63+
error("No `provider.toggle` available — run `:checkhealth opencode` for details", 0)
6864
end
6965
end
7066

@@ -74,21 +70,19 @@ function M.start()
7470
provider:start()
7571
started = true
7672
else
77-
vim.notify(
78-
"No `provider.start` available — start `opencode` yourself, or configure `vim.g.opencode_opts.provider`, or install `snacks.nvim` to use the default provider",
79-
vim.log.levels.ERROR,
80-
{ title = "opencode" }
81-
)
73+
error("No `provider.start` available — run `:checkhealth opencode` for details", 0)
8274
end
8375
end
8476

8577
---Show `opencode` via `opts.provider`.
86-
--- Only called if `provider.toggle` or `provider.start` was previously called.
78+
---Only called if `provider.toggle` or `provider.start` was previously called.
8779
function M.show()
88-
if provider and provider.show and started then
89-
provider:show()
90-
else
91-
-- no-op - this is more of a preference than a requirement.
80+
if started then
81+
if provider and provider.show then
82+
provider:show()
83+
else
84+
error("No `provider.show` available — run `:checkhealth opencode` for details", 0)
85+
end
9286
end
9387
end
9488

0 commit comments

Comments
 (0)