Skip to content

Commit 6eeaeb3

Browse files
committed
Improve error messages to include function names
Add missing if constexpr for std::string_view
1 parent 7f545f7 commit 6eeaeb3

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Shared/mods/deathmatch/logic/lua/CLuaFunctionParser.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct CLuaFunctionParserBase
4040
template <typename T>
4141
inline SString TypeToName()
4242
{
43-
if constexpr (std::is_same_v<T, std::string>)
43+
if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>)
4444
return "string";
4545
else if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double> || std::is_same_v<T, short> ||
4646
std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned short>)
@@ -131,7 +131,8 @@ struct CLuaFunctionParserBase
131131
{
132132
SString strReceived = ReadParameterAsString(L, index);
133133
SString strExpected = TypeToName<T>();
134-
SString strMessage("Expected %s at argument %d, got %s", strExpected.c_str(), index, strReceived.c_str());
134+
SString strMessage("Bad argument @ '%s' [Expected %s at argument %d, got %s]", lua_tostring(L, lua_upvalueindex(1)), strExpected.c_str(), index,
135+
strReceived.c_str());
135136
strError = strMessage;
136137
return T{};
137138
}
@@ -173,7 +174,7 @@ struct CLuaFunctionParserBase
173174
{
174175
int iArgument = lua_type(L, index);
175176
// primitive types
176-
if constexpr (std::is_same_v<T, std::string>)
177+
if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>)
177178
return (iArgument == LUA_TSTRING || iArgument == LUA_TNUMBER);
178179
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double> || std::is_same_v<T, short> ||
179180
std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned short>)
@@ -259,7 +260,7 @@ struct CLuaFunctionParserBase
259260
return dummy_type{};
260261
// primitive types are directly popped
261262
else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, int> || std::is_same_v<T, short> || std::is_same_v<T, bool> ||
262-
std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned short>)
263+
std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned short> || std::is_same_v<T, std::string_view>)
263264
return lua::PopPrimitive<T>(L, index);
264265
// floats/doubles may not be NaN
265266
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>)
@@ -268,6 +269,8 @@ struct CLuaFunctionParserBase
268269
if (std::isnan(value))
269270
{
270271
SString strMessage("Expected number at argument %d, got NaN", index);
272+
SString strMessage("Bad argument @ '%s' [Expected number at argument %d, got NaN]", lua_tostring(L, lua_upvalueindex(1)), strExpected.c_str(),
273+
index);
271274
strError = strMessage;
272275
}
273276
return value;
@@ -283,7 +286,8 @@ struct CLuaFunctionParserBase
283286
{
284287
SString strReceived = ReadParameterAsString(L, index);
285288
SString strExpected = GetEnumTypeName((T)0);
286-
SString strMessage("Expected %s at argument %d, got %s", strExpected.c_str(), index, strReceived.c_str());
289+
SString strMessage("Bad argument @ '%s' [Expected %s at argument %d, got %s]", lua_tostring(L, lua_upvalueindex(1)), strExpected.c_str(), index,
290+
strReceived.c_str());
287291
strError = strMessage;
288292
return static_cast<T>(0);
289293
}
@@ -370,7 +374,8 @@ struct CLuaFunctionParserBase
370374
{
371375
SString strReceived = isLightUserData ? GetUserDataClassName(pValue, L) : GetUserDataClassName(*(void**)pValue, L);
372376
SString strExpected = GetClassTypeName((T)0);
373-
SString strMessage("Expected %s at argument %d, got %s", strExpected.c_str(), index, strReceived.c_str());
377+
SString strMessage("Bad argument @ '%s' [Expected %s at argument %d, got %s]", lua_tostring(L, lua_upvalueindex(1)),
378+
strExpected.c_str(), index, strReceived.c_str());
374379
strError = strMessage;
375380
return nullptr;
376381
}
@@ -421,7 +426,7 @@ struct CLuaFunctionParser<ErrorOnFailure, ReturnOnFailure, Func> : CLuaFunctionP
421426
}
422427
catch (std::invalid_argument& e)
423428
{
424-
// This exception can only be thrown from the called function
429+
// This exception can be thrown from the called function
425430
// as an additional way to provide further argument errors
426431
strError = e.what();
427432
}

0 commit comments

Comments
 (0)