Skip to content

Commit 36aa338

Browse files
JH23XJonas Holzner
authored andcommitted
server : handle exception on wrong type in request (ggml-org#6452)
Co-authored-by: Jonas Holzner <[email protected]>
1 parent bf2c8a3 commit 36aa338

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

examples/server/utils.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ extern bool server_log_json;
5252
#define LOG_WARNING(MSG, ...) server_log("WARN", __func__, __LINE__, MSG, __VA_ARGS__)
5353
#define LOG_INFO( MSG, ...) server_log("INFO", __func__, __LINE__, MSG, __VA_ARGS__)
5454

55+
static inline void server_log(const char *level, const char *function, int line, const char *message, const nlohmann::ordered_json &extra);
56+
5557
template <typename T>
5658
static T json_value(const json &body, const std::string &key, const T &default_value) {
5759
// Fallback null to default value
58-
return body.contains(key) && !body.at(key).is_null()
59-
? body.value(key, default_value)
60-
: default_value;
60+
if (body.contains(key) && !body.at(key).is_null()){
61+
try {
62+
return body.value(key, default_value);
63+
}
64+
catch (nlohmann::json_abi_v3_11_3::detail::type_error const&){
65+
std::string message = "Wrong type supplied for parameter '" + key + "'. Expected '" + typeid(default_value).name() + "', using default value.";
66+
server_log("WARN", __func__, __LINE__, message.c_str(), body);
67+
return default_value;
68+
}
69+
} else {
70+
return default_value;
71+
}
6172
}
6273

6374
static inline void server_log(const char *level, const char *function, int line, const char *message, const nlohmann::ordered_json &extra) {

0 commit comments

Comments
 (0)