Skip to content
Snippets Groups Projects
Commit fc78a6d6 authored by Roman Khabibov's avatar Roman Khabibov Committed by Kirill Yukhin
Browse files

json: make error messages more readable

Print tokens themselves instead of token names "T_*" in the error
messages.

Part of #4339
parent 15a929f2
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ end
tap.test("json", function(test)
local serializer = require('json')
test:plan(40)
test:plan(51)
test:test("unsigned", common.test_unsigned, serializer)
test:test("signed", common.test_signed, serializer)
......@@ -154,4 +154,34 @@ tap.test("json", function(test)
_, err_msg = pcall(serializer.decode, '{"hello": "world",\n 100: 200}')
test:ok(string.find(err_msg, 'line 2 at character 2') ~= nil,
'mistake on second line')
--
-- gh-4339: Make sure that tokens 'T_*' are absent in error
-- messages and a context is printed.
--
_, err_msg = pcall(serializer.decode, '{{: "world"}')
test:ok(string.find(err_msg, '\'{\'') ~= nil, '"{" instead of T_OBJ_BEGIN')
_, err_msg = pcall(serializer.decode, '{"a": "world"}}')
test:ok(string.find(err_msg, '\'}\'') ~= nil, '"}" instead of T_OBJ_END')
_, err_msg = pcall(serializer.decode, '{[: "world"}')
test:ok(string.find(err_msg, '\'[\'', 1, true) ~= nil,
'"[" instead of T_ARR_BEGIN')
_, err_msg = pcall(serializer.decode, '{]: "world"}')
test:ok(string.find(err_msg, '\']\'', 1, true) ~= nil,
'"]" instead of T_ARR_END')
_, err_msg = pcall(serializer.decode, '{1: "world"}')
test:ok(string.find(err_msg, 'int') ~= nil, 'int instead of T_INT')
_, err_msg = pcall(serializer.decode, '{1.0: "world"}')
test:ok(string.find(err_msg, 'number') ~= nil, 'number instead of T_NUMBER')
_, err_msg = pcall(serializer.decode, '{true: "world"}')
test:ok(string.find(err_msg, 'boolean') ~= nil,
'boolean instead of T_BOOLEAN')
_, err_msg = pcall(serializer.decode, '{null: "world"}')
test:ok(string.find(err_msg, 'null') ~= nil, 'null instead of T_NULL')
_, err_msg = pcall(serializer.decode, '{:: "world"}')
test:ok(string.find(err_msg, 'colon') ~= nil, 'colon instead of T_COLON')
_, err_msg = pcall(serializer.decode, '{,: "world"}')
test:ok(string.find(err_msg, 'comma') ~= nil, 'comma instead of T_COMMA')
_, err_msg = pcall(serializer.decode, '{')
test:ok(string.find(err_msg, 'end') ~= nil, 'end instead of T_END')
end)
......@@ -75,23 +75,23 @@ typedef enum {
} json_token_type_t;
static const char *json_token_type_name[] = {
"T_OBJ_BEGIN",
"T_OBJ_END",
"T_ARR_BEGIN",
"T_ARR_END",
"T_STRING",
"T_UINT",
"T_INT",
"T_NUMBER",
"T_BOOLEAN",
"T_NULL",
"T_COLON",
"T_COMMA",
"T_END",
"T_WHITESPACE",
"T_LINEFEED",
"T_ERROR",
"T_UNKNOWN",
"'{'",
"'}'",
"'['",
"']'",
"string",
"unsigned int",
"int",
"number",
"boolean",
"null",
"colon",
"comma",
"end",
"whitespace",
"line feed",
"error",
"unknown symbol",
NULL
};
......@@ -920,7 +920,7 @@ static void json_parse_object_context(lua_State *l, json_parse_t *json)
}
if (token.type != T_COMMA)
json_throw_parse_error(l, json, "comma or object end", &token);
json_throw_parse_error(l, json, "comma or '}'", &token);
json_next_token(json, &token);
}
......@@ -960,7 +960,7 @@ static void json_parse_array_context(lua_State *l, json_parse_t *json)
}
if (token.type != T_COMMA)
json_throw_parse_error(l, json, "comma or array end", &token);
json_throw_parse_error(l, json, "comma or ']'", &token);
json_next_token(json, &token);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment