Skip to content

Commit facc4f5

Browse files
committed
refactor: simplify error handling in main and OpenAI modules 🔧
- streamline clipboard operations with chained error handling - adjust debug logging formatting in OpenAI model check Signed-off-by: mingcheng <mingcheng@apache.org>
1 parent f1d1ff7 commit facc4f5

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/main.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use aigitcommit::built_info::{PKG_NAME, PKG_VERSION};
1616
use aigitcommit::cli::Cli;
1717
use aigitcommit::git::message::GitMessage;
1818
use aigitcommit::git::repository::Repository;
19-
use aigitcommit::openai;
2019
use aigitcommit::openai::OpenAI;
2120
use arboard::Clipboard;
2221
use async_openai::types::{
@@ -28,9 +27,7 @@ use std::io::Write;
2827
use std::path::Path;
2928
use tracing::{Level, debug, error, info, trace};
3029

31-
use aigitcommit::utils::{
32-
OutputFormat, check_env_variables, env, format_openai_error, save_to_file, should_signoff,
33-
};
30+
use aigitcommit::utils::{OutputFormat, check_env_variables, env, save_to_file, should_signoff};
3431

3532
// Constants for better performance and maintainability
3633
const DEFAULT_MODEL: &str = "gpt-5";
@@ -119,10 +116,7 @@ async fn main() -> Result<()> {
119116
];
120117

121118
// Send the request to OpenAI API and get the response
122-
let result = client
123-
.chat(&model_name, messages)
124-
.await
125-
.map_err(|e| format_openai_error(e))?;
119+
let result = client.chat(&model_name, messages).await?;
126120

127121
let (title, content) = result
128122
.split_once("\n\n")
@@ -139,9 +133,10 @@ async fn main() -> Result<()> {
139133

140134
// Copy the commit message to clipboard if the --copy option is enabled
141135
if cli.copy_to_clipboard {
142-
let mut clipboard = Clipboard::new()
143-
.map_err(|e| format!("failed to initialize clipboard: {e}"))?;
144-
clipboard.set_text(message.to_string())
136+
let mut clipboard =
137+
Clipboard::new().map_err(|e| format!("failed to initialize clipboard: {e}"))?;
138+
clipboard
139+
.set_text(message.to_string())
145140
.map_err(|e| format!("failed to copy to clipboard: {e}"))?;
146141
writeln!(
147142
std::io::stdout(),

src/openai.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ impl OpenAI {
118118
/// Check if the OpenAI API and specified model are reachable and available.
119119
pub async fn check_model(&self, model_name: &str) -> Result<(), Box<dyn Error>> {
120120
let list = self.client.models().list().await?;
121-
121+
122122
debug!(
123123
"Available models: {:?}",
124124
list.data.iter().map(|m| &m.id).collect::<Vec<_>>()
125125
);
126-
126+
127127
if list.data.iter().any(|model| model.id == model_name) {
128128
debug!("OpenAI API is reachable and model {model_name} is available");
129129
Ok(())

0 commit comments

Comments
 (0)