error_handling/
error_handling.rs1use gpt5::{Gpt5Client, Gpt5Model};
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 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 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 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 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 println!("\nš Testing very long input...");
46 let long_input = "Hello ".repeat(10000); 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 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 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 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}