From 926a0b992d29fb4f608c1a81d040283a13cd0071 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Sat, 13 Jul 2024 22:56:04 +0200 Subject: [PATCH] improve error messages when probing JSON path value type --- src/Utils.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 5e26dfdf..94f80e57 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -112,6 +112,12 @@ std::optional getFromString(char const* val) return res; } +template +char const* getTypename(); + +template<> +char const* getTypename() { return "float"; } + template std::pair Utils::getJsonValueByPath(JsonDocument const& root, String const& path) { @@ -179,15 +185,17 @@ std::pair Utils::getJsonValueByPath(JsonDocument const& root, String } if (!value.is()) { - snprintf(errBuffer, kErrBufferSize, "Value '%s' at JSON path '%s' is not " - "of the expected type", value.as().c_str(), path.c_str()); + snprintf(errBuffer, kErrBufferSize, "Value '%s' at JSON path '%s' is " + "neither a string nor of type %s", value.as().c_str(), + path.c_str(), getTypename()); return { T(), String(errBuffer) }; } auto res = getFromString(value.as()); if (!res.has_value()) { snprintf(errBuffer, kErrBufferSize, "String '%s' at JSON path '%s' cannot " - "be converted to the expected type", value.as().c_str(), path.c_str()); + "be converted to %s", value.as().c_str(), path.c_str(), + getTypename()); return { T(), String(errBuffer) }; }