Skip to content

Commit 007fbf3

Browse files
committed
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 593800a commit 007fbf3

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

cv/cv.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,17 +2420,17 @@ cv_check_array(lua_State *L, struct cv_ctx *ctx,
24202420
const struct cv_node *n)
24212421
{
24222422
if (lua_type(L, data_idx) != LUA_TTABLE) {
2423-
int det = cv_ctx_push_error(L, ctx, n,
2424-
"TYPE_ERROR",
2425-
"Wrong type, expected array,"
2426-
" got map");
2423+
char msg[256];
2424+
const char *actual_type =
2425+
lua_typename(L, lua_type(L, data_idx));
2426+
snprintf(msg, sizeof(msg), "Wrong type, expected array, got %s",
2427+
actual_type);
2428+
int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg);
24272429
if (det != 0) {
24282430
lua_pushstring(L, "array");
24292431
lua_setfield(L, det,
24302432
"expected_type");
2431-
lua_pushstring(L,
2432-
lua_typename(L,
2433-
lua_type(L, data_idx)));
2433+
lua_pushstring(L, actual_type);
24342434
lua_setfield(L, det,
24352435
"actual_type");
24362436
lua_pushvalue(L, data_idx);
@@ -2590,10 +2590,12 @@ cv_check_map(lua_State *L, struct cv_ctx *ctx,
25902590
const struct cv_node *n)
25912591
{
25922592
if (lua_type(L, data_idx) != LUA_TTABLE) {
2593-
int det = cv_ctx_push_error(L, ctx, n,
2594-
"TYPE_ERROR",
2595-
"Wrong type, expected map, got"
2596-
" non-table");
2593+
char msg[256];
2594+
const char *actual_type =
2595+
lua_typename(L, lua_type(L, data_idx));
2596+
snprintf(msg, sizeof(msg), "Wrong type, expected map, got %s",
2597+
actual_type);
2598+
int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg);
25972599
if (det != 0) {
25982600
lua_pushstring(L, "map");
25992601
lua_setfield(L, det,

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)