summaryrefslogtreecommitdiff
path: root/examples/server/utils.hpp
diff options
context:
space:
mode:
authorJH23X <165871467+JH23X@users.noreply.github.com>2024-04-03 20:09:52 +0200
committerGitHub <noreply@github.com>2024-04-03 21:09:52 +0300
commit60cdf40cc32f0ad4cb11e0ca8fd38f3b93d8d640 (patch)
tree669f02e375a965c22d6637d45ddac218e68145b6 /examples/server/utils.hpp
parentbb43cf7e9d86d69ffd9c7f008f75db890a35b45a (diff)
server : handle exception on wrong type in request (#6452)
Co-authored-by: Jonas Holzner <jonas.holzner.external@hensoldt.net>
Diffstat (limited to 'examples/server/utils.hpp')
-rw-r--r--examples/server/utils.hpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/examples/server/utils.hpp b/examples/server/utils.hpp
index 7d9ab622..47cc53c2 100644
--- a/examples/server/utils.hpp
+++ b/examples/server/utils.hpp
@@ -49,12 +49,23 @@ extern bool server_log_json;
#define LOG_WARNING(MSG, ...) server_log("WARN", __func__, __LINE__, MSG, __VA_ARGS__)
#define LOG_INFO( MSG, ...) server_log("INFO", __func__, __LINE__, MSG, __VA_ARGS__)
+static inline void server_log(const char *level, const char *function, int line, const char *message, const nlohmann::ordered_json &extra);
+
template <typename T>
static T json_value(const json &body, const std::string &key, const T &default_value) {
// Fallback null to default value
- return body.contains(key) && !body.at(key).is_null()
- ? body.value(key, default_value)
- : default_value;
+ if (body.contains(key) && !body.at(key).is_null()){
+ try {
+ return body.value(key, default_value);
+ }
+ catch (nlohmann::json_abi_v3_11_3::detail::type_error const&){
+ std::string message = "Wrong type supplied for parameter '" + key + "'. Expected '" + typeid(default_value).name() + "', using default value.";
+ server_log("WARN", __func__, __LINE__, message.c_str(), body);
+ return default_value;
+ }
+ } else {
+ return default_value;
+ }
}
static inline void server_log(const char *level, const char *function, int line, const char *message, const nlohmann::ordered_json &extra) {