Skip to content

Commit

Permalink
feat(core): better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Nov 21, 2024
1 parent cfb9817 commit 5cdc368
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 12 deletions.
13 changes: 7 additions & 6 deletions lua/neolog/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ local function build_batch_log_statement(log_template, batch)
local current_line = vim.fn.getpos(".")[2]
local result1, insert_cursor_offset = resolve_template_placeholders(result, {
identifier = function()
error("%identifier placeholder can only be used inside %repeat placeholder")
vim.notify("neolog: Cannot use %identifier placeholder outside %repeat placeholder", vim.log.levels.ERROR)
return "%identifier"
end,
line_number = tostring(current_line + 1),
})
Expand All @@ -369,21 +370,21 @@ end
local function get_lang_log_template(template_set, kind)
local lang = get_lang(vim.bo.filetype)
if not lang then
vim.notify("Cannot determine language for current buffer", vim.log.levels.ERROR)
vim.notify("neolog: Treesitter cannot determine language for current buffer", vim.log.levels.ERROR)
return
end

local log_template_set = (kind == "single" and M.log_templates or M.batch_log_templates)[template_set]
if not log_template_set then
vim.notify(string.format("Log template '%s' is not found", template_set), vim.log.levels.ERROR)
vim.notify(string.format("neolog: Log template '%s' is not found", template_set), vim.log.levels.ERROR)
return
end

local log_template_lang = log_template_set[lang]
if not log_template_lang then
vim.notify(
string.format(
"%s '%s' does not have '%s' language template",
"neolog: %s '%s' does not have '%s' language template",
kind == "single" and "Log template" or "Batch log template",
template_set,
lang
Expand Down Expand Up @@ -454,7 +455,7 @@ function M.__insert_batch_log(_)
opts = vim.tbl_deep_extend("force", { template = "default" }, opts or {})

if #M.batch == 0 then
vim.notify("Log batch is empty", vim.log.levels.INFO)
vim.notify("neolog: Log batch is empty", vim.log.levels.INFO)
return
end

Expand Down Expand Up @@ -485,7 +486,7 @@ end
function M.__add_log_targets_to_batch()
local lang = get_lang(vim.bo.filetype)
if not lang then
vim.notify("Cannot determine language for current buffer", vim.log.levels.ERROR)
vim.notify("neolog: Treesitter cannot determine language for current buffer", vim.log.levels.ERROR)
return
end

Expand Down
140 changes: 134 additions & 6 deletions tests/neolog/actions/neolog_actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,58 @@ describe("neolog.actions.insert_log", function()
]],
})
end)

describe("handles user errors", function()
it("notifies when the filetype is not recognized", function()
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "unknown",
expected = function()
assert.error_matches(function()
actions.insert_log({ position = "below" })
end, "neolog: Treesitter cannot determine language for current buffer")
end,
})
end)

it("notifies when the log template is not found", function()
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "javascript",
expected = function()
assert.error_matches(function()
actions.insert_log({ template = "unknown", position = "below" })
end, "neolog: Log template 'unknown' is not found")
end,
})
end)

it("notifies when the filetype is not recognized", function()
neolog.setup({
log_templates = {
testing = {},
},
})
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "javascript",
expected = function()
assert.error_matches(function()
actions.insert_log({ template = "testing", position = "below" })
end, "neolog: Log template 'testing' does not have 'javascript' language template")
end,
})
end)
end)
end)

describe("neolog.actions.insert_batch_log", function()
Expand Down Expand Up @@ -427,7 +479,7 @@ describe("neolog.actions.insert_batch_log", function()
end,
expected = function()
assert.spy(notify_spy).was_called(1)
assert.spy(notify_spy).was_called_with("Log batch is empty", vim.log.levels.INFO)
assert.spy(notify_spy).was_called_with("neolog: Log batch is empty", vim.log.levels.INFO)
end,
})

Expand All @@ -449,18 +501,22 @@ describe("neolog.actions.insert_batch_log", function()
const baz = "baz"
]]

local notify_spy = spy.on(vim, "notify")

helper.assert_scenario({
input = input,
filetype = "javascript",
action = function()
vim.cmd("normal! V2j")
actions.add_log_targets_to_batch()
actions.__insert_batch_log()
end,
expected = function()
assert.has_error(function()
-- Use the internal function to capture the error message
actions.__insert_batch_log()
end, "%identifier placeholder can only be used inside %repeat placeholder")
assert.spy(notify_spy).was_called(1)
assert
.spy(notify_spy)
.was_called_with("neolog: Cannot use %identifier placeholder outside %repeat placeholder", vim.log.levels.ERROR)
notify_spy:clear()
end,
})
end)
Expand Down Expand Up @@ -490,9 +546,64 @@ describe("neolog.actions.insert_batch_log", function()
-- Dot repeat the action. Now the batch is empty, it should notify the user
vim.cmd("normal! .")
assert.spy(notify_spy).was_called(1)
assert.spy(notify_spy).was_called_with("Log batch is empty", vim.log.levels.INFO)
assert.spy(notify_spy).was_called_with("neolog: Log batch is empty", vim.log.levels.INFO)
notify_spy:clear()
end)

describe("handles user errors", function()
it("notifies when the filetype is not recognized", function()
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "unknown",
expected = function()
assert.error_matches(function()
actions.add_log_targets_to_batch()
actions.insert_batch_log()
end, "neolog: Treesitter cannot determine language for current buffer")
end,
})
end)

it("notifies when the log template is not found", function()
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "javascript",
expected = function()
assert.error_matches(function()
actions.add_log_targets_to_batch()
actions.insert_batch_log({ template = "unknown" })
end, "neolog: Log template 'unknown' is not found")
end,
})
end)

it("notifies when the filetype is not recognized", function()
neolog.setup({
batch_log_templates = {
testing = {},
},
})
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "javascript",
expected = function()
assert.error_matches(function()
actions.add_log_targets_to_batch()
actions.insert_batch_log({ template = "testing" })
end, "neolog: Batch log template 'testing' does not have 'javascript' language template")
end,
})
end)
end)
end)

describe("neolog.actions.add_log_targets_to_batch", function()
Expand Down Expand Up @@ -561,4 +672,21 @@ describe("neolog.actions.add_log_targets_to_batch", function()
end,
})
end)

describe("handles user errors", function()
it("notifies when the filetype is not recognized", function()
helper.assert_scenario({
input = [[
// Comment
const fo|o = bar + baz
]],
filetype = "unknown",
expected = function()
assert.error_matches(function()
actions.add_log_targets_to_batch()
end, "neolog: Treesitter cannot determine language for current buffer")
end,
})
end)
end)
end)

0 comments on commit 5cdc368

Please sign in to comment.