From 1e9adb88e8932321e3afb1a3148dae1011f05af3 Mon Sep 17 00:00:00 2001 From: Mergen Imeev Date: Thu, 26 Mar 2026 13:43:55 +0300 Subject: [PATCH] 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`. --- cv/cv.c | 28 ++++++++++++++-------------- test/compat_test.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/cv/cv.c b/cv/cv.c index bfc0d36..48fe150 100644 --- a/cv/cv.c +++ b/cv/cv.c @@ -2420,17 +2420,17 @@ cv_check_array(lua_State *L, struct cv_ctx *ctx, const struct cv_node *n) { if (lua_type(L, data_idx) != LUA_TTABLE) { - int det = cv_ctx_push_error(L, ctx, n, - "TYPE_ERROR", - "Wrong type, expected array," - " got map"); + char msg[256]; + const char *actual_type = + lua_typename(L, lua_type(L, data_idx)); + snprintf(msg, sizeof(msg), "Wrong type, expected array, got %s", + actual_type); + int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg); if (det != 0) { lua_pushstring(L, "array"); lua_setfield(L, det, "expected_type"); - lua_pushstring(L, - lua_typename(L, - lua_type(L, data_idx))); + lua_pushstring(L, actual_type); lua_setfield(L, det, "actual_type"); lua_pushvalue(L, data_idx); @@ -2590,17 +2590,17 @@ cv_check_map(lua_State *L, struct cv_ctx *ctx, const struct cv_node *n) { if (lua_type(L, data_idx) != LUA_TTABLE) { - int det = cv_ctx_push_error(L, ctx, n, - "TYPE_ERROR", - "Wrong type, expected map, got" - " non-table"); + char msg[256]; + const char *actual_type = + lua_typename(L, lua_type(L, data_idx)); + snprintf(msg, sizeof(msg), "Wrong type, expected map, got %s", + actual_type); + int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg); if (det != 0) { lua_pushstring(L, "map"); lua_setfield(L, det, "expected_type"); - lua_pushstring(L, - lua_typename(L, - lua_type(L, data_idx))); + lua_pushstring(L, actual_type); lua_setfield(L, det, "actual_type"); lua_pushvalue(L, data_idx); diff --git a/test/compat_test.lua b/test/compat_test.lua index c157594..5d54a4c 100644 --- a/test/compat_test.lua +++ b/test/compat_test.lua @@ -897,4 +897,31 @@ function g.test_array_map_rejected() t.assert_equals(e2[1].type, 'ARRAY_EXPECTED') end +-- ------------------------------------------------------- +-- array: make sure the error message is correct if a non-array +-- was provided instead of an array. +-- ------------------------------------------------------- + +function g.test_array_type_error_message() + local r, e = cv.check(1, {type = 'array'}) + local exp = { + details = {actual_type = "number", expected_type = "array", value = 1}, + message = "Wrong type, expected array, got number", + path = "$", + type = "TYPE_ERROR", + } + t.assert_equals(r, nil) + t.assert_equals(e, {exp}) + + r, e = cv.check(1, {type = 'map'}) + exp = { + details = {actual_type = "number", expected_type = "map", value = 1}, + message = "Wrong type, expected map, got number", + path = "$", + type = "TYPE_ERROR", + } + t.assert_equals(r, nil) + t.assert_equals(e, {exp}) +end + -- vim: ts=4 sts=4 sw=4 et