Skip to content

Add error handling for LSP configuration access in health module #56

@coderabbitai

Description

@coderabbitai

Description

The current implementation of M.register_lsp in plugins/nobbz/lua/nobbz/health.lua assumes that the LSP configuration exists and has the expected structure. This can lead to runtime errors if:

  1. The LSP configuration doesn't exist
  2. The LSP configuration doesn't have a cmd field or it's empty
  3. The LSP configuration doesn't have a filetypes field or it's empty

Proposed Solution

Add error handling to safely handle these cases:

M.register_lsp = function(lsp)
  local config = require("lspconfig")[lsp]
  if not config then
    vim.notify("LSP configuration for " .. lsp .. " not found", vim.log.levels.WARN)
    return
  end

  if not config.cmd or #config.cmd == 0 then
    vim.notify("No command found for LSP " .. lsp, vim.log.levels.WARN)
    return
  end

  if not config.filetypes or #config.filetypes == 0 then
    vim.notify("No filetypes found for LSP " .. lsp, vim.log.levels.WARN)
    return
  end

  local program = config.cmd[1]
  local pattern = config.filetypes[1]

  M.register_program(program, false)

  vim.api.nvim_create_autocmd("FileType", {
    pattern = pattern,
    callback = function()
      M.register_program(program, true)
    end,
  })
end

References

Assignee

@NobbZ

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions