Skip to content

Commit 6cabfbe

Browse files
ImeevMAlocker
authored andcommitted
Fix error message for wrong type
Before this patch, if a non-array value was provided instead of an array, an error message would appear indicating that a `map` was given instead of an array, even if the value provided was not a `map`. Now the correct type name is specified. The situation was similar with the `map`, but the predefined type was set to `non-table`.
1 parent 0541ea9 commit 6cabfbe

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

cv/cv.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,17 +2421,17 @@ cv_check_array(lua_State *L, struct cv_ctx *ctx,
24212421
const struct cv_node *n)
24222422
{
24232423
if (lua_type(L, data_idx) != LUA_TTABLE) {
2424-
int det = cv_ctx_push_error(L, ctx, n,
2425-
"TYPE_ERROR",
2426-
"Wrong type, expected array,"
2427-
" got map");
2424+
char msg[256];
2425+
const char *actual_type =
2426+
lua_typename(L, lua_type(L, data_idx));
2427+
snprintf(msg, sizeof(msg), "Wrong type, expected array, got %s",
2428+
actual_type);
2429+
int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg);
24282430
if (det != 0) {
24292431
lua_pushstring(L, "array");
24302432
lua_setfield(L, det,
24312433
"expected_type");
2432-
lua_pushstring(L,
2433-
lua_typename(L,
2434-
lua_type(L, data_idx)));
2434+
lua_pushstring(L, actual_type);
24352435
lua_setfield(L, det,
24362436
"actual_type");
24372437
lua_pushvalue(L, data_idx);
@@ -2591,17 +2591,17 @@ cv_check_map(lua_State *L, struct cv_ctx *ctx,
25912591
const struct cv_node *n)
25922592
{
25932593
if (lua_type(L, data_idx) != LUA_TTABLE) {
2594-
int det = cv_ctx_push_error(L, ctx, n,
2595-
"TYPE_ERROR",
2596-
"Wrong type, expected map, got"
2597-
" non-table");
2594+
char msg[256];
2595+
const char *actual_type =
2596+
lua_typename(L, lua_type(L, data_idx));
2597+
snprintf(msg, sizeof(msg), "Wrong type, expected map, got %s",
2598+
actual_type);
2599+
int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg);
25982600
if (det != 0) {
25992601
lua_pushstring(L, "map");
26002602
lua_setfield(L, det,
26012603
"expected_type");
2602-
lua_pushstring(L,
2603-
lua_typename(L,
2604-
lua_type(L, data_idx)));
2604+
lua_pushstring(L, actual_type);
26052605
lua_setfield(L, det,
26062606
"actual_type");
26072607
lua_pushvalue(L, data_idx);

test/compat_test.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,4 +897,31 @@ function g.test_array_map_rejected()
897897
t.assert_equals(e2[1].type, 'ARRAY_EXPECTED')
898898
end
899899

900+
-- -------------------------------------------------------
901+
-- array: make sure the error message is correct if a non-array
902+
-- was provided instead of an array.
903+
-- -------------------------------------------------------
904+
905+
function g.test_array_type_error_message()
906+
local r, e = cv.check(1, {type = 'array'})
907+
local exp = {
908+
details = {actual_type = "number", expected_type = "array", value = 1},
909+
message = "Wrong type, expected array, got number",
910+
path = "$",
911+
type = "TYPE_ERROR",
912+
}
913+
t.assert_equals(r, nil)
914+
t.assert_equals(e, {exp})
915+
916+
r, e = cv.check(1, {type = 'map'})
917+
exp = {
918+
details = {actual_type = "number", expected_type = "map", value = 1},
919+
message = "Wrong type, expected map, got number",
920+
path = "$",
921+
type = "TYPE_ERROR",
922+
}
923+
t.assert_equals(r, nil)
924+
t.assert_equals(e, {exp})
925+
end
926+
900927
-- vim: ts=4 sts=4 sw=4 et

0 commit comments

Comments
 (0)