Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lua/json/decode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ local function generateDecoder(lexer, options)
local function diagnostic_decoder(data)
local state = decode_state.create(options)
local parsed = diagnostic_parser:match(data)
if not parsed then
error("Invalid JSON data")
end
local last_pos = 1
local i = 0
local ok, err = pcall(function()
Expand All @@ -133,7 +130,8 @@ local function generateDecoder(lexer, options)
local line, col, char, last_line = util.get_invalid_character_info(data, last_pos)
error(err .. (" @ character: %i %i:%i [%s] line:\n%s"):format(last_pos, line, col, char, last_line))
end
return state.previous
-- This line should never be reached since diagnostic_decoder is only run on failure paths
assert(false, "Diagnostic decoder finished without throwing an error")
end

local decoder = function(data)
Expand Down
22 changes: 22 additions & 0 deletions tests/lunit-error-reporting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,25 @@ end
function test_missing_array_value()
assert_decode_error('[1,, 2]', "Expected value in array", nil, 1, 4)
end

-- Test: expected utility helper with multiple arguments
function test_util_expected_helper()
local util = require("json.decode.util")
local pattern = util.expected("foo", "bar")
local ok, err = pcall(function()
pattern:match("data")
end)
assert_false(ok)
assert_not_nil(string_find(tostring(err), "expected one of 'foo','bar'", 1, true))
end

-- Test: expected utility helper with single argument
function test_util_expected_single_helper()
local util = require("json.decode.util")
local pattern = util.expected("baz")
local ok, err = pcall(function()
pattern:match("data")
end)
assert_false(ok)
assert_not_nil(string_find(tostring(err), "expected 'baz'", 1, true))
end