improve error messages when probing JSON path value type

This commit is contained in:
Bernhard Kirchen 2024-07-13 22:56:04 +02:00 committed by Bernhard Kirchen
parent 9e7a0bca31
commit 926a0b992d

View File

@ -112,6 +112,12 @@ std::optional<float> getFromString(char const* val)
return res;
}
template<typename T>
char const* getTypename();
template<>
char const* getTypename<float>() { return "float"; }
template<typename T>
std::pair<T, String> Utils::getJsonValueByPath(JsonDocument const& root, String const& path)
{
@ -179,15 +185,17 @@ std::pair<T, String> Utils::getJsonValueByPath(JsonDocument const& root, String
}
if (!value.is<char const*>()) {
snprintf(errBuffer, kErrBufferSize, "Value '%s' at JSON path '%s' is not "
"of the expected type", value.as<String>().c_str(), path.c_str());
snprintf(errBuffer, kErrBufferSize, "Value '%s' at JSON path '%s' is "
"neither a string nor of type %s", value.as<String>().c_str(),
path.c_str(), getTypename<T>());
return { T(), String(errBuffer) };
}
auto res = getFromString<T>(value.as<char const*>());
if (!res.has_value()) {
snprintf(errBuffer, kErrBufferSize, "String '%s' at JSON path '%s' cannot "
"be converted to the expected type", value.as<String>().c_str(), path.c_str());
"be converted to %s", value.as<String>().c_str(), path.c_str(),
getTypename<T>());
return { T(), String(errBuffer) };
}