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

Function demonstrate_streaming_errors

examples/error_handling.cpp:114–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

112}
113
114void demonstrate_streaming_errors() {
115 std::cout << "4. Streaming Error Handling\n";
116 std::cout << "===========================\n\n";
117
118 std::cout << "Testing streaming with invalid model:\n";
119
120 auto client = ai::openai::create_client();
121 ai::GenerateOptions gen_options;
122 gen_options.model = "invalid-streaming-model";
123 gen_options.prompt = "Tell me a story";
124 ai::StreamOptions stream_options(std::move(gen_options));
125 auto stream = client.stream_text(stream_options);
126
127 // Check for immediate errors
128 if (stream.has_error()) {
129 std::cout << "Stream error detected: " << stream.error_message() << "\n\n";
130 } else {
131 // Process stream and handle errors during iteration
132 for (const auto& event : stream) {
133 if (event.is_error()) {
134 std::cout << "Stream event error: " << event.error.value_or("Unknown")
135 << "\n";
136 break;
137 } else if (event.is_text_delta()) {
138 std::cout << event.text_delta;
139 } else if (event.is_finish()) {
140 std::cout << "\nStream completed successfully\n";
141 break;
142 }
143 }
144 std::cout << "\n";
145 }
146}
147
148void demonstrate_exception_handling() {
149 std::cout << "5. Exception Handling\n";

Callers 1

mainFunction · 0.85

Calls 7

has_errorMethod · 0.80
is_errorMethod · 0.80
is_text_deltaMethod · 0.80
is_finishMethod · 0.80
create_clientFunction · 0.50
stream_textMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected