error_handling/
error_handling.rs

1//! Error handling example for GPT-5 client
2//!
3//! This example demonstrates proper error handling patterns
4//! Run with: cargo run --example error_handling
5
6use gpt5::{Gpt5Client, Gpt5Model};
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Test with invalid API key
11    println!("šŸ”‘ Testing with invalid API key...");
12    let invalid_client = Gpt5Client::new("invalid-key".to_string());
13
14    match invalid_client.simple(Gpt5Model::Gpt5Nano, "Hello").await {
15        Ok(response) => println!("Unexpected success: {}", response),
16        Err(e) => {
17            println!("āŒ Expected error: {}", e);
18            // Check error type by string content
19            let error_str = e.to_string();
20            if error_str.contains("401") || error_str.contains("Unauthorized") {
21                println!("   Error type: Authentication error");
22            } else if error_str.contains("timeout") {
23                println!("   Error type: Timeout error");
24            } else if error_str.contains("network") {
25                println!("   Error type: Network error");
26            } else {
27                println!("   Error type: Other error");
28            }
29        }
30    }
31
32    // Test with valid API key (if available)
33    if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
34        println!("\nāœ… Testing with valid API key...");
35        let client = Gpt5Client::new(api_key);
36
37        // Test empty input
38        println!("šŸ“ Testing empty input...");
39        match client.simple(Gpt5Model::Gpt5Nano, "").await {
40            Ok(response) => println!("Response: {}", response),
41            Err(e) => println!("āŒ Error with empty input: {}", e),
42        }
43
44        // Test very long input
45        println!("\nšŸ“ Testing very long input...");
46        let long_input = "Hello ".repeat(10000); // Very long string
47        match client.simple(Gpt5Model::Gpt5Nano, &long_input).await {
48            Ok(response) => println!("Response length: {} chars", response.len()),
49            Err(e) => println!("āŒ Error with long input: {}", e),
50        }
51
52        // Test normal usage
53        println!("\nāœ… Testing normal usage...");
54        match client
55            .simple(Gpt5Model::Gpt5Nano, "Say hello in 3 different languages")
56            .await
57        {
58            Ok(response) => println!("āœ… Success: {}", response),
59            Err(e) => println!("āŒ Unexpected error: {}", e),
60        }
61    } else {
62        println!("āš ļø  OPENAI_API_KEY not set, skipping valid key tests");
63    }
64
65    // Demonstrate error type checking
66    println!("\nšŸ” Error type checking example...");
67    let client = Gpt5Client::new("test-key".to_string());
68
69    match client.simple(Gpt5Model::Gpt5Nano, "test").await {
70        Ok(_) => println!("Unexpected success"),
71        Err(e) => {
72            // Check if it's a network error
73            if e.to_string().contains("401") || e.to_string().contains("Unauthorized") {
74                println!("šŸ” Authentication error detected");
75            } else if e.to_string().contains("timeout") {
76                println!("ā° Timeout error detected");
77            } else if e.to_string().contains("network") {
78                println!("🌐 Network error detected");
79            } else {
80                println!("ā“ Other error: {}", e);
81            }
82        }
83    }
84
85    Ok(())
86}