MCPcopy Create free account
hub / github.com/ClickHouse/ai-sdk-cpp / parse_standard_error_response

Function parse_standard_error_response

src/utils/response_utils.h:16–53  ·  view source on GitHub ↗

Parse a generic error response that follows the common pattern of { "error": { "message": "...", "type": "..." } }

Source from the content-addressed store, hash-verified

14// Parse a generic error response that follows the common pattern of
15// { "error": { "message": "...", "type": "..." } }
16inline GenerateResult parse_standard_error_response(
17 const std::string& provider_name,
18 int status_code,
19 const std::string& body) {
20 ai::logger::log_debug("Parsing error response - status: {}, body: {}",
21 status_code, body);
22
23 GenerateResult result;
24 result.is_retryable = ai::is_status_code_retryable(status_code);
25
26 try {
27 auto json = nlohmann::json::parse(body);
28 if (json.contains("error")) {
29 auto& error = json["error"];
30 std::string message = error.value("message", "Unknown error");
31 std::string type = error.value("type", "");
32
33 std::string full_error = provider_name + " API error (" +
34 std::to_string(status_code) + "): " + message +
35 (type.empty() ? "" : " [" + type + "]");
36 ai::logger::log_error("{} API error parsed: {}", provider_name,
37 full_error);
38
39 result.error = full_error;
40 return result;
41 }
42 } catch (...) {
43 // If JSON parsing fails, return raw error
44 ai::logger::log_debug("Failed to parse error response as JSON");
45 }
46
47 std::string raw_error =
48 "HTTP " + std::to_string(status_code) + " error: " + body;
49 ai::logger::log_error("{} API raw error: {}", provider_name, raw_error);
50
51 result.error = raw_error;
52 return result;
53}
54
55} // namespace utils
56} // namespace ai

Calls 3

log_debugFunction · 0.85
is_status_code_retryableFunction · 0.85
log_errorFunction · 0.85

Tested by

no test coverage detected