Skip to content

Commit 057213a

Browse files
ssjolearyclaude
andcommitted
fix: address PR review feedback on editor/getDiagnostics
- send_response guards against non-running server process - add send_error_response for spec-compliant JSON-RPC error replies - unknown methods return nil -> -32601 Method Not Found error response - pcall-wrap error-path sends to prevent crash on shutdown race - rename test variable count -> diagnostics for clarity Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 127a305 commit 057213a

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

lua/eca/editor.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function M.handle_request(message)
3939
if message.method == "editor/getDiagnostics" then
4040
return get_diagnostics(message.params or {})
4141
end
42-
return {}
42+
return nil
4343
end
4444

4545
return M

lua/eca/server.lua

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@ function M:handle_message(message)
267267
local ok, result = pcall(self.on_request, self, message)
268268
if not ok then
269269
Logger.error("on_request error: " .. tostring(result))
270-
self:send_response(id, {})
270+
pcall(self.send_error_response, self, id, -32603, "Internal error")
271+
return
272+
end
273+
if result == nil then
274+
pcall(self.send_error_response, self, id, -32601, "Method not found")
271275
return
272276
end
273277
local ok2, err = pcall(self.send_response, self, id, result)
@@ -286,12 +290,30 @@ end
286290
---@param id integer|string
287291
---@param result table
288292
function M:send_response(id, result)
293+
if not self:is_running() then
294+
Logger.error("send_response: server not running, dropping response for id=" .. tostring(id))
295+
return
296+
end
289297
local message = { jsonrpc = "2.0", id = id, result = result }
290298
local json = vim.json.encode(message)
291299
local content = string.format("Content-Length: %d\r\n\r\n%s", #json, json)
292300
self.process:write(content)
293301
end
294302

303+
---@param id integer|string
304+
---@param code integer JSON-RPC error code
305+
---@param msg string
306+
function M:send_error_response(id, code, msg)
307+
if not self:is_running() then
308+
Logger.error("send_error_response: server not running, dropping error for id=" .. tostring(id))
309+
return
310+
end
311+
local message = { jsonrpc = "2.0", id = id, error = { code = code, message = msg } }
312+
local json = vim.json.encode(message)
313+
local content = string.format("Content-Length: %d\r\n\r\n%s", #json, json)
314+
self.process:write(content)
315+
end
316+
295317
---@return integer
296318
function M:get_next_id()
297319
self.next_id = self.next_id + 1

tests/test_editor_diagnostics.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ local T = MiniTest.new_set({
1414
T["editor"] = MiniTest.new_set()
1515

1616
T["editor"]["returns empty diagnostics when buffer not found"] = function()
17-
local count = child.lua_get([[
17+
local diagnostics = child.lua_get([[
1818
require("eca.editor").handle_request({
1919
method = "editor/getDiagnostics",
2020
params = { uri = "file:///nonexistent/file.lua" },
2121
}).diagnostics
2222
]])
23-
eq(#count, 0)
23+
eq(#diagnostics, 0)
2424
end
2525

2626
T["editor"]["maps severity levels correctly"] = function()
@@ -113,11 +113,11 @@ T["editor"]["uri field is present on each diagnostic"] = function()
113113
eq(child.lua_get("_G.uri_result[2]"), "file:///tmp/eca_test_uri.lua")
114114
end
115115

116-
T["editor"]["returns empty table for unknown method"] = function()
117-
local count = child.lua_get([[
118-
vim.tbl_count(require("eca.editor").handle_request({ method = "editor/unknown", params = {} }))
116+
T["editor"]["returns nil for unknown method"] = function()
117+
local is_nil = child.lua_get([[
118+
require("eca.editor").handle_request({ method = "editor/unknown", params = {} }) == nil
119119
]])
120-
eq(count, 0)
120+
eq(is_nil, true)
121121
end
122122

123123
return T

0 commit comments

Comments
 (0)