@@ -315,10 +315,10 @@ inline void ensureSuccessfulResponse(const json& response)
315315 {
316316 const std::string code = error_it->value (" code" , " unknown_error" );
317317 const std::string message = error_it->value (" message" , " request failed" );
318- throw JsonInvokeError (code, message);
318+ throw JsonInvokeError (ErrorCategory::InvalidResponse, code, message);
319319 }
320320
321- throw JsonInvokeError (" invalid_response" , " response is missing a successful result state" );
321+ throw JsonInvokeError (ErrorCategory::InvalidResponse, " invalid_response" , " response is missing a successful result state" );
322322}
323323
324324inline const json& responseValue (const json& response)
@@ -328,7 +328,7 @@ inline const json& responseValue(const json& response)
328328 auto value_it = response.find (" value" );
329329 if (value_it == response.end ())
330330 {
331- throw JsonInvokeError (" invalid_response" , " successful response does not contain a 'value' field" );
331+ throw JsonInvokeError (ErrorCategory::InvalidResponse, " invalid_response" , " successful response does not contain a 'value' field" );
332332 }
333333
334334 return *value_it;
@@ -375,7 +375,7 @@ class JsonInvokeResult {
375375 }
376376 catch (const std::exception& e)
377377 {
378- throw JsonInvokeError (" conversion_failed" , " failed to convert response value: " + std::string (e.what ()));
378+ throw JsonInvokeError (ErrorCategory::ConversionFailed, " conversion_failed" , " failed to convert response value: " + std::string (e.what ()));
379379 }
380380 }
381381
@@ -451,7 +451,7 @@ class JsonTypeRegistry {
451451 {
452452 if (!value.is_null ())
453453 {
454- throw JsonInvokeError (" conversion_failed" , " void type expects null JSON" );
454+ throw JsonInvokeError (ErrorCategory::ConversionFailed, " conversion_failed" , " void type expects null JSON" );
455455 }
456456 return std::any{};
457457 }
@@ -461,6 +461,7 @@ class JsonTypeRegistry {
461461 if (it == from_json_.end ())
462462 {
463463 throw JsonInvokeError (
464+ ErrorCategory::UnsupportedType,
464465 " unsupported_type" ,
465466 " JSON input conversion is not registered for C++ type '" + std::string (expected_type.name ()) + " '" );
466467 }
@@ -476,6 +477,7 @@ class JsonTypeRegistry {
476477 catch (const std::exception& e)
477478 {
478479 throw JsonInvokeError (
480+ ErrorCategory::ConversionFailed,
479481 " conversion_failed" ,
480482 " JSON input conversion failed for C++ type '" + std::string (expected_type.name ()) + " ': " + e.what ());
481483 }
@@ -493,6 +495,7 @@ class JsonTypeRegistry {
493495 if (it == to_json_.end ())
494496 {
495497 throw JsonInvokeError (
498+ ErrorCategory::UnsupportedType,
496499 " unsupported_type" ,
497500 " JSON output conversion is not registered for C++ type '" + std::string (actual_type.name ()) + " '" );
498501 }
@@ -508,6 +511,7 @@ class JsonTypeRegistry {
508511 catch (const std::exception& e)
509512 {
510513 throw JsonInvokeError (
514+ ErrorCategory::ConversionFailed,
511515 " conversion_failed" ,
512516 " JSON output conversion failed for C++ type '" + std::string (actual_type.name ()) + " ': " + e.what ());
513517 }
@@ -946,7 +950,7 @@ class BasicJsonInvokeAdapter {
946950 {
947951 if (!request.is_object ())
948952 {
949- throw JsonInvokeError (" invalid_request" , " request must be a JSON object" );
953+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " request must be a JSON object" );
950954 }
951955 return request;
952956 }
@@ -965,7 +969,7 @@ class BasicJsonInvokeAdapter {
965969
966970 if (!it->is_string ())
967971 {
968- throw JsonInvokeError (" invalid_request" , " field '" + std::string (field) + " ' must be a string" );
972+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " field '" + std::string (field) + " ' must be a string" );
969973 }
970974
971975 return it->get <std::string>();
@@ -976,24 +980,24 @@ class BasicJsonInvokeAdapter {
976980 {
977981 if (!function_it->is_object ())
978982 {
979- throw JsonInvokeError (" invalid_request" , " field 'function' must be an object" );
983+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " field 'function' must be an object" );
980984 }
981985
982986 auto name_it = function_it->find (" name" );
983987 if (name_it == function_it->end ())
984988 {
985- throw JsonInvokeError (" invalid_request" , " field 'function.name' is required" );
989+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " field 'function.name' is required" );
986990 }
987991
988992 if (!name_it->is_string ())
989993 {
990- throw JsonInvokeError (" invalid_request" , " field 'function.name' must be a string" );
994+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " field 'function.name' must be a string" );
991995 }
992996
993997 return name_it->get <std::string>();
994998 }
995999
996- throw JsonInvokeError (" invalid_request" , " request must contain a string field 'name'" );
1000+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " request must contain a string field 'name'" );
9971001 }
9981002
9991003 static json parseArgumentsString (std::string_view raw)
@@ -1004,7 +1008,7 @@ class BasicJsonInvokeAdapter {
10041008 }
10051009 catch (const json::parse_error& e)
10061010 {
1007- throw JsonInvokeError (" invalid_request" , " tool arguments string is not valid JSON: " + std::string (e.what ()));
1011+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " tool arguments string is not valid JSON: " + std::string (e.what ()));
10081012 }
10091013 }
10101014
@@ -1037,7 +1041,7 @@ class BasicJsonInvokeAdapter {
10371041 {
10381042 if (!function_it->is_object ())
10391043 {
1040- throw JsonInvokeError (" invalid_request" , " field 'function' must be an object" );
1044+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " field 'function' must be an object" );
10411045 }
10421046
10431047 auto function_args_it = function_it->find (" arguments" );
@@ -1064,7 +1068,7 @@ class BasicJsonInvokeAdapter {
10641068 }
10651069 catch (const std::exception& e)
10661070 {
1067- throw JsonInvokeError (" function_not_found" , e.what ());
1071+ throw JsonInvokeError (ErrorCategory::FunctionNotFound, " function_not_found" , e.what ());
10681072 }
10691073 }
10701074
@@ -1082,7 +1086,7 @@ class BasicJsonInvokeAdapter {
10821086 }
10831087 catch (const std::exception& e)
10841088 {
1085- throw JsonInvokeError (" call_failed" , e.what ());
1089+ throw JsonInvokeError (ErrorCategory::CallFailed, " call_failed" , e.what ());
10861090 }
10871091 }
10881092
@@ -1093,6 +1097,7 @@ class BasicJsonInvokeAdapter {
10931097 if (!json_type_registry_.canRead (info.arg_types [index]))
10941098 {
10951099 throw JsonInvokeError (
1100+ ErrorCategory::UnsupportedType,
10961101 " unsupported_type" ,
10971102 " argument " + std::to_string (index) + " ('" + argumentLabel (info, index) + " ') uses unsupported C++ type '" +
10981103 std::string (info.arg_types [index].name ()) + " ' for JSON input conversion" );
@@ -1102,6 +1107,7 @@ class BasicJsonInvokeAdapter {
11021107 if (!json_type_registry_.canWrite (info.ret_type ))
11031108 {
11041109 throw JsonInvokeError (
1110+ ErrorCategory::UnsupportedType,
11051111 " unsupported_type" ,
11061112 " return type '" + std::string (info.ret_type .name ()) + " ' is not registered for JSON output conversion" );
11071113 }
@@ -1124,14 +1130,15 @@ class BasicJsonInvokeAdapter {
11241130 return {};
11251131 }
11261132
1127- throw JsonInvokeError (" invalid_request" , " field 'args' must be an array or object" );
1133+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " field 'args' must be an array or object" );
11281134 }
11291135
11301136 std::vector<std::any> packPositionalArgs (const FunctionInfo& info, const json& args) const
11311137 {
11321138 if (args.size () > info.arg_types .size ())
11331139 {
11341140 throw JsonInvokeError (
1141+ ErrorCategory::InvalidRequest,
11351142 " invalid_request" ,
11361143 " argument count mismatch: expected " + std::to_string (info.arg_types .size ()) +
11371144 " , got " + std::to_string (args.size ()));
@@ -1142,6 +1149,7 @@ class BasicJsonInvokeAdapter {
11421149 if (isArgumentRequired (info, index))
11431150 {
11441151 throw JsonInvokeError (
1152+ ErrorCategory::InvalidRequest,
11451153 " invalid_request" ,
11461154 " missing JSON argument for parameter '" + argumentLabel (info, index) + " '" );
11471155 }
@@ -1166,7 +1174,7 @@ class BasicJsonInvokeAdapter {
11661174 {
11671175 if (!args.is_object ())
11681176 {
1169- throw JsonInvokeError (" invalid_request" , " named JSON arguments must be a JSON object" );
1177+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " named JSON arguments must be a JSON object" );
11701178 }
11711179
11721180 for (auto it = args.begin (); it != args.end (); ++it)
@@ -1183,7 +1191,7 @@ class BasicJsonInvokeAdapter {
11831191
11841192 if (!known)
11851193 {
1186- throw JsonInvokeError (" invalid_request" , " unexpected JSON argument '" + it.key () + " '" );
1194+ throw JsonInvokeError (ErrorCategory::InvalidRequest, " invalid_request" , " unexpected JSON argument '" + it.key () + " '" );
11871195 }
11881196 }
11891197
@@ -1199,6 +1207,7 @@ class BasicJsonInvokeAdapter {
11991207 if (isArgumentRequired (info, index))
12001208 {
12011209 throw JsonInvokeError (
1210+ ErrorCategory::InvalidRequest,
12021211 " invalid_request" ,
12031212 " missing JSON argument for parameter '" + parameter_name + " '" );
12041213 }
@@ -1225,7 +1234,7 @@ class BasicJsonInvokeAdapter {
12251234 }
12261235 catch (const JsonInvokeError& e)
12271236 {
1228- throw JsonInvokeError (e.code (), " argument " + std::to_string (index) + " ('" + label + " '): " + e.what ());
1237+ throw JsonInvokeError (e.category (), e. code (), " argument " + std::to_string (index) + " ('" + label + " '): " + e.what ());
12291238 }
12301239 }
12311240
0 commit comments