fix(lsp): display initialization errors (#25409)

This commit is contained in:
Maria José Solano 2023-10-11 13:38:58 -07:00 committed by GitHub
parent 37da0bc0c6
commit 2fde6295df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -189,7 +189,9 @@ lsp.client_errors = tbl_extend(
'error', 'error',
lsp_rpc.client_errors, lsp_rpc.client_errors,
vim.tbl_add_reverse_lookup({ vim.tbl_add_reverse_lookup({
ON_INIT_CALLBACK_ERROR = table.maxn(lsp_rpc.client_errors) + 1, BEFORE_INIT_CALLBACK_ERROR = table.maxn(lsp_rpc.client_errors) + 1,
ON_INIT_CALLBACK_ERROR = table.maxn(lsp_rpc.client_errors) + 2,
ON_ATTACH_ERROR = table.maxn(lsp_rpc.client_errors) + 3,
}) })
) )
@ -1172,6 +1174,16 @@ function lsp.start_client(config)
return nil, lsp.rpc_response_error(protocol.ErrorCodes.MethodNotFound) return nil, lsp.rpc_response_error(protocol.ErrorCodes.MethodNotFound)
end end
--- Logs the given error to the LSP log and to the error buffer.
--- @param code integer Error code
--- @param err any Error arguments
local function write_error(code, err)
if log.error() then
log.error(log_prefix, 'on_error', { code = lsp.client_errors[code], err = err })
end
err_message(log_prefix, ': Error ', lsp.client_errors[code], ': ', vim.inspect(err))
end
---@private ---@private
--- Invoked when the client operation throws an error. --- Invoked when the client operation throws an error.
--- ---
@ -1180,10 +1192,7 @@ function lsp.start_client(config)
---@see vim.lsp.rpc.client_errors for possible errors. Use ---@see vim.lsp.rpc.client_errors for possible errors. Use
---`vim.lsp.rpc.client_errors[code]` to get a human-friendly name. ---`vim.lsp.rpc.client_errors[code]` to get a human-friendly name.
function dispatch.on_error(code, err) function dispatch.on_error(code, err)
if log.error() then write_error(code, err)
log.error(log_prefix, 'on_error', { code = lsp.client_errors[code], err = err })
end
err_message(log_prefix, ': Error ', lsp.client_errors[code], ': ', vim.inspect(err))
if config.on_error then if config.on_error then
local status, usererr = pcall(config.on_error, code, err) local status, usererr = pcall(config.on_error, code, err)
if not status then if not status then
@ -1391,8 +1400,10 @@ function lsp.start_client(config)
trace = valid_traces[config.trace] or 'off', trace = valid_traces[config.trace] or 'off',
} }
if config.before_init then if config.before_init then
-- TODO(ashkan) handle errors here. local status, err = pcall(config.before_init, initialize_params, config)
pcall(config.before_init, initialize_params, config) if not status then
write_error(lsp.client_errors.BEFORE_INIT_CALLBACK_ERROR, err)
end
end end
--- @param method string --- @param method string
@ -1440,7 +1451,7 @@ function lsp.start_client(config)
if config.on_init then if config.on_init then
local status, err = pcall(config.on_init, client, result) local status, err = pcall(config.on_init, client, result)
if not status then if not status then
pcall(handlers.on_error, lsp.client_errors.ON_INIT_CALLBACK_ERROR, err) write_error(lsp.client_errors.ON_INIT_CALLBACK_ERROR, err)
end end
end end
local _ = log.info() local _ = log.info()
@ -1714,8 +1725,10 @@ function lsp.start_client(config)
}) })
if config.on_attach then if config.on_attach then
-- TODO(ashkan) handle errors. local status, err = pcall(config.on_attach, client, bufnr)
pcall(config.on_attach, client, bufnr) if not status then
write_error(lsp.client_errors.ON_ATTACH_ERROR, err)
end
end end
-- schedule the initialization of semantic tokens to give the above -- schedule the initialization of semantic tokens to give the above