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

Function demonstrate_api_errors

examples/error_handling.cpp:21–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19#include <ai/ai.h>
20
21void demonstrate_api_errors() {
22 std::cout << "1. API Error Handling\n";
23 std::cout << "======================\n\n";
24
25 // Test with invalid API key
26 std::cout << "Testing with invalid model name:\n";
27 auto client = ai::openai::create_client();
28 ai::GenerateOptions options1;
29 options1.model = "invalid-model-2024";
30 options1.prompt = "Hello, world!";
31 auto result1 = client.generate_text(options1);
32
33 if (!result1) {
34 std::cout << "Expected error: " << result1.error_message() << "\n\n";
35 }
36
37 // Test with empty prompt
38 std::cout << "Testing with empty prompt:\n";
39 ai::GenerateOptions options2;
40 options2.model = ai::openai::models::kGpt54;
41 options2.prompt = "";
42 auto result2 = client.generate_text(options2);
43
44 if (!result2) {
45 std::cout << "Expected error: " << result2.error_message() << "\n\n";
46 }
47
48 // Test with malformed model identifier
49 std::cout << "Testing with malformed model identifier:\n";
50 ai::GenerateOptions options3;
51 options3.model = "";
52 options3.prompt = "Hello, world!";
53 auto result3 = client.generate_text(options3);
54
55 if (!result3) {
56 std::cout << "Expected error: " << result3.error_message() << "\n\n";
57 }
58}
59
60void demonstrate_validation() {
61 std::cout << "2. Input Validation\n";

Callers 1

mainFunction · 0.85

Calls 3

create_clientFunction · 0.50
generate_textMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected