From 950d1504e8a3b16f07c13bc50ed8a22ae5c21a51 Mon Sep 17 00:00:00 2001 From: Muhammad Nawaz Date: Tue, 10 Jun 2025 20:48:53 +0500 Subject: [PATCH] Return error string from GetValidators --- src/lua_oasvalidator_imp.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lua_oasvalidator_imp.cpp b/src/lua_oasvalidator_imp.cpp index 6ff3794..7ce9f72 100644 --- a/src/lua_oasvalidator_imp.cpp +++ b/src/lua_oasvalidator_imp.cpp @@ -56,6 +56,7 @@ int lua_GetValidators(lua_State* L) const char* oas_specs = luaL_checkstring(L, 1); std::unordered_map> method_map = {}; if (num_args == 2) { + luaL_checktype(L, 2, LUA_TTABLE); LuaTableToUnorderedMapSet(L, 2, method_map); } auto* oas_validators_imp = new OASValidatorImp(oas_specs, method_map); @@ -63,20 +64,30 @@ int lua_GetValidators(lua_State* L) OASValidatorImp** ud = reinterpret_cast(lua_newuserdata(L, sizeof(*ud))); if (!ud) { delete oas_validators_imp; - luaL_error(L, "Out of memory"); + lua_pushnil(L); + lua_pushliteral(L, "Out of memory"); + return 2; } *ud = oas_validators_imp; luaL_getmetatable(L, METATABLE_NAME); lua_setmetatable(L, -2); + lua_pushnil(L); + return 2; } else { lua_pushnil(L); + lua_pushliteral(L, "Failed to create validators"); + return 2; } } catch (const ValidatorInitExc& ex) { lua_pushnil(L); - luaL_error(L, ex.what()); + lua_pushstring(L, ex.what()); + return 2; + } catch (const std::exception& ex) { + lua_pushnil(L); + lua_pushstring(L, ex.what()); + return 2; } - return 1; } int metamethod_gc(lua_State* L)