Skip to content

Commit 4343fb7

Browse files
Copilotoleander
andcommitted
Fix API key error handling: propagate authentication failures as errors instead of warnings
Co-authored-by: oleander <220827+oleander@users.noreply.github.com>
1 parent 90cd57b commit 4343fb7

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

src/commit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ pub async fn generate(patch: String, remaining_tokens: usize, model: Model, sett
121121
Ok(message) => return Ok(openai::Response { response: message }),
122122
Err(e) => {
123123
// Check if it's an API key error
124-
if e.to_string().contains("invalid_api_key") || e.to_string().contains("Incorrect API key") {
124+
if e.to_string().contains("invalid_api_key") ||
125+
e.to_string().contains("Incorrect API key") ||
126+
e.to_string().contains("OpenAI API authentication failed") {
125127
bail!("Invalid OpenAI API key. Please check your API key configuration.");
126128
}
127129
log::warn!("Multi-step generation with custom settings failed: {e}");
@@ -149,7 +151,9 @@ pub async fn generate(patch: String, remaining_tokens: usize, model: Model, sett
149151
Ok(message) => return Ok(openai::Response { response: message }),
150152
Err(e) => {
151153
// Check if it's an API key error
152-
if e.to_string().contains("invalid_api_key") || e.to_string().contains("Incorrect API key") {
154+
if e.to_string().contains("invalid_api_key") ||
155+
e.to_string().contains("Incorrect API key") ||
156+
e.to_string().contains("OpenAI API authentication failed") {
153157
bail!("Invalid OpenAI API key. Please check your API key configuration.");
154158
}
155159
log::warn!("Multi-step generation failed: {e}");

src/multi_step_integration.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,16 @@ pub async fn generate_commit_message_multi_step(
8989
file_analyses.push((file, analysis));
9090
}
9191
Err(e) => {
92-
// Check if it's an API key error - if so, propagate it immediately
92+
// Check if it's an API key or authentication error - if so, propagate it immediately
9393
let error_str = e.to_string();
94-
if error_str.contains("invalid_api_key") || error_str.contains("Incorrect API key") || error_str.contains("Invalid API key") {
95-
return Err(e);
94+
if error_str.contains("invalid_api_key") ||
95+
error_str.contains("Incorrect API key") ||
96+
error_str.contains("Invalid API key") ||
97+
error_str.contains("authentication") ||
98+
error_str.contains("unauthorized") ||
99+
// Detect HTTP errors that typically indicate auth issues when calling OpenAI
100+
(error_str.contains("http error") && error_str.contains("error sending request")) {
101+
return Err(anyhow::anyhow!("OpenAI API authentication failed: {}. Please check your API key configuration.", e));
96102
}
97103
log::warn!("Failed to analyze file {}: {}", file.path, e);
98104
// Continue with other files even if one fails

src/openai.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ pub async fn call_with_config(request: Request, config: OpenAIConfig) -> Result<
209209
Ok(message) => return Ok(Response { response: message }),
210210
Err(e) => {
211211
// Check if it's an API key error and propagate it
212-
if e.to_string().contains("invalid_api_key") || e.to_string().contains("Incorrect API key") {
212+
if e.to_string().contains("invalid_api_key") ||
213+
e.to_string().contains("Incorrect API key") ||
214+
e.to_string().contains("OpenAI API authentication failed") {
213215
return Err(e);
214216
}
215217
log::warn!("Multi-step approach failed, falling back to single-step: {e}");

tests/api_key_error_test.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use async_openai::Client;
2+
use async_openai::config::OpenAIConfig;
3+
use ai::multi_step_integration::generate_commit_message_multi_step;
4+
5+
#[tokio::test]
6+
async fn test_invalid_api_key_propagates_error() {
7+
// Initialize logging to capture warnings
8+
let _ = env_logger::builder().is_test(true).try_init();
9+
10+
// Create a client with an invalid API key that matches the issue
11+
let config = OpenAIConfig::new().with_api_key("dl://BA7invalid_key_here");
12+
let client = Client::with_config(config);
13+
14+
let example_diff = r#"diff --git a/test.txt b/test.txt
15+
new file mode 100644
16+
index 0000000..0000000
17+
--- /dev/null
18+
+++ b/test.txt
19+
@@ -0,0 +1 @@
20+
+Hello World
21+
"#;
22+
23+
// This should fail with an API key error, not log a warning and continue
24+
let result = generate_commit_message_multi_step(&client, "gpt-4o-mini", example_diff, Some(72)).await;
25+
26+
// Verify the behavior - it should return an error, not continue with other files
27+
assert!(result.is_err(), "Expected API key error to be propagated as error, not warning");
28+
29+
let error_message = result.unwrap_err().to_string();
30+
println!("Actual error message: '{}'", error_message);
31+
32+
// Now it should properly detect authentication failures
33+
assert!(
34+
error_message.contains("OpenAI API authentication failed") ||
35+
error_message.contains("API key"),
36+
"Expected error message to indicate authentication failure, got: {}",
37+
error_message
38+
);
39+
}

0 commit comments

Comments
 (0)