fix(lsp): Ensure human readable errors are printed

`return err_message(tostring(err))` caused errors to be printed as
`table: 0x123456789` instead of showing the error code and error
message.

This also removes some `if err` blocks that never got called because at
the end of `handlers.lua` all the handlers are wrapped with logic that
adds generic error handling.
This commit is contained in:
Mathias Fussenegger 2021-07-11 11:22:35 +02:00
parent 256570a7a6
commit c21a6972a0

View File

@ -18,10 +18,8 @@ local function err_message(...)
end end
--@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand --@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand
M['workspace/executeCommand'] = function(err, _) M['workspace/executeCommand'] = function()
if err then -- Error handling is done implicitly by wrapping all handlers; see end of this file
error("Could not execute code action: "..err.message)
end
end end
-- @msg of type ProgressParams -- @msg of type ProgressParams
@ -158,13 +156,12 @@ M['workspace/applyEdit'] = function(_, _, workspace_edit)
end end
--@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_configuration --@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_configuration
M['workspace/configuration'] = function(err, _, params, client_id) M['workspace/configuration'] = function(_, _, params, client_id)
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
if not client then if not client then
err_message("LSP[id=", client_id, "] client has shut down after sending the message") err_message("LSP[id=", client_id, "] client has shut down after sending the message")
return return
end end
if err then error(vim.inspect(err)) end
if not params.items then if not params.items then
return {} return {}
end end
@ -199,11 +196,7 @@ end
--@param map_result function `((resp, bufnr) -> list)` to convert the response --@param map_result function `((resp, bufnr) -> list)` to convert the response
--@param entity name of the resource used in a `not found` error message --@param entity name of the resource used in a `not found` error message
local function response_to_qflist(map_result, entity) local function response_to_qflist(map_result, entity)
return function(err, _, result, _, bufnr) return function(_, _, result, _, bufnr)
if err then
vim.notify(err.message, vim.log.levels.ERROR)
return
end
if not result or vim.tbl_isempty(result) then if not result or vim.tbl_isempty(result) then
vim.notify('No ' .. entity .. ' found') vim.notify('No ' .. entity .. ' found')
else else
@ -454,7 +447,12 @@ for k, fn in pairs(M) do
}) })
if err then if err then
return err_message(tostring(err)) -- LSP spec:
-- interface ResponseError:
-- code: integer;
-- message: string;
-- data?: string | number | boolean | array | object | null;
return err_message(tostring(err.code) .. ': ' .. err.message)
end end
return fn(err, method, params, client_id, bufnr, config) return fn(err, method, params, client_id, bufnr, config)