|
| 1 | +//! Error handling utilities for the git-ai CLI tool. |
| 2 | +//! |
| 3 | +//! This module provides helpers for detecting and handling specific error types, |
| 4 | +//! particularly authentication failures from the OpenAI API. |
| 5 | +
|
| 6 | +use anyhow::Error; |
| 7 | + |
| 8 | +/// Checks if an error represents an OpenAI API authentication failure. |
| 9 | +/// |
| 10 | +/// This function detects various authentication failure patterns including: |
| 11 | +/// - OpenAI-specific API key errors (invalid_api_key, incorrect API key) |
| 12 | +/// - Generic authentication/authorization failures |
| 13 | +/// - HTTP-level errors that typically indicate authentication issues when calling OpenAI |
| 14 | +/// |
| 15 | +/// # Arguments |
| 16 | +/// |
| 17 | +/// * `error` - The error to check |
| 18 | +/// |
| 19 | +/// # Returns |
| 20 | +/// |
| 21 | +/// `true` if the error appears to be an authentication failure, `false` otherwise |
| 22 | +/// |
| 23 | +/// # Examples |
| 24 | +/// |
| 25 | +/// ``` |
| 26 | +/// use anyhow::anyhow; |
| 27 | +/// use ai::error::is_openai_auth_error; |
| 28 | +/// |
| 29 | +/// let error = anyhow!("invalid_api_key: Incorrect API key provided"); |
| 30 | +/// assert!(is_openai_auth_error(&error)); |
| 31 | +/// ``` |
| 32 | +pub fn is_openai_auth_error(error: &Error) -> bool { |
| 33 | + let msg = error.to_string().to_lowercase(); |
| 34 | + |
| 35 | + // OpenAI-specific API key errors |
| 36 | + msg.contains("invalid_api_key") || |
| 37 | + msg.contains("incorrect api key") || |
| 38 | + msg.contains("openai api authentication failed") || |
| 39 | + |
| 40 | + // Generic auth failures (scoped to avoid false positives) |
| 41 | + (msg.contains("authentication") && msg.contains("openai")) || |
| 42 | + (msg.contains("unauthorized") && msg.contains("openai")) || |
| 43 | + |
| 44 | + // HTTP errors that typically indicate auth issues with OpenAI |
| 45 | + // This pattern catches connection issues when the API key is malformed |
| 46 | + (msg.contains("http error") && msg.contains("error sending request")) |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + use anyhow::anyhow; |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_detects_invalid_api_key() { |
| 56 | + let error = anyhow!("invalid_api_key: Incorrect API key provided"); |
| 57 | + assert!(is_openai_auth_error(&error)); |
| 58 | + } |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_detects_incorrect_api_key() { |
| 62 | + let error = anyhow!("Incorrect API key provided: sk-xxxxx"); |
| 63 | + assert!(is_openai_auth_error(&error)); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn test_detects_openai_auth_failed() { |
| 68 | + let error = anyhow!("OpenAI API authentication failed: http error"); |
| 69 | + assert!(is_openai_auth_error(&error)); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_detects_http_error_sending_request() { |
| 74 | + let error = anyhow!("http error: error sending request"); |
| 75 | + assert!(is_openai_auth_error(&error)); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_detects_openai_specific_auth() { |
| 80 | + let error = anyhow!("OpenAI authentication failed"); |
| 81 | + assert!(is_openai_auth_error(&error)); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_ignores_generic_auth_errors() { |
| 86 | + // Should not match generic auth errors without OpenAI context |
| 87 | + let error = anyhow!("Database authentication timeout"); |
| 88 | + assert!(!is_openai_auth_error(&error)); |
| 89 | + |
| 90 | + let error = anyhow!("OAuth2 unauthorized redirect"); |
| 91 | + assert!(!is_openai_auth_error(&error)); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_ignores_unrelated_errors() { |
| 96 | + let error = anyhow!("File not found"); |
| 97 | + assert!(!is_openai_auth_error(&error)); |
| 98 | + } |
| 99 | +} |
0 commit comments