99 * File Created: 2025-03-01 17:17:30
1010 *
1111 * Modified By: mingcheng ([email protected] ) 12- * Last Modified: 2025-03-04 16:30:20
12+ * Last Modified: 2025-03-16 23:11:04
1313 */
1414
1515use aigitcommit:: cli:: Cli ;
1616use aigitcommit:: openai:: OpenAI ;
1717use aigitcommit:: { git, openai} ;
1818use arboard:: Clipboard ;
19+ use async_openai:: error:: OpenAIError ;
1920use async_openai:: types:: {
2021 ChatCompletionRequestSystemMessageArgs , ChatCompletionRequestUserMessageArgs ,
2122} ;
@@ -33,17 +34,17 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
3334 let cli = Cli :: parse ( ) ;
3435
3536 // Initialize logging
36- tracing_subscriber :: fmt ( )
37- . with_max_level ( if cli . verbose {
38- trace ! ( "Verbose mode enabled, set the log level to TRACE. It will makes a little bit noise." ) ;
39- Level :: TRACE
40- } else {
41- debug ! ( "Verbose mode disabled, set the default log level to WARN" ) ;
42- Level :: WARN
43- } )
44- . without_time ( )
45- . with_target ( false )
46- . init ( ) ;
37+ if cli . verbose {
38+ tracing_subscriber :: fmt ( )
39+ . with_max_level ( Level :: TRACE )
40+ . without_time ( )
41+ . with_target ( false )
42+ . init ( ) ;
43+
44+ trace ! (
45+ "Verbose mode enabled, set the log level to TRACE. It will makes a little bit noise."
46+ ) ;
47+ }
4748
4849 // Check if the specified path is a valid directory
4950 let repo_dir = fs:: canonicalize ( & cli. repo_path ) ?;
@@ -67,11 +68,11 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
6768 // Get the last 5 commit logs
6869 // if the repository has less than 5 commits, it will return all logs
6970 let logs = repository. get_logs ( 5 ) ?;
70- debug ! ( "Got logs size is {}" , logs. len( ) ) ;
71+ debug ! ( "got logs size is {}" , logs. len( ) ) ;
7172
7273 // If git commit log is empty, return error
7374 if logs. is_empty ( ) {
74- return Err ( "No commit logs found" . into ( ) ) ;
75+ return Err ( "no commit logs found" . into ( ) ) ;
7576 }
7677
7778 // Instantiate OpenAI client, ready to send requests to the OpenAI API
@@ -106,7 +107,24 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
106107 ] ;
107108
108109 // Send the request to OpenAI API and get the response
109- let result = client. chat ( & model_name. to_string ( ) , messages) . await ?;
110+ let result = match client. chat ( & model_name. to_string ( ) , messages) . await {
111+ Ok ( s) => s,
112+ Err ( e) => {
113+ let message = match e {
114+ OpenAIError :: Reqwest ( _) | OpenAIError :: StreamError ( _) => {
115+ "network request error" . to_string ( )
116+ }
117+ OpenAIError :: JSONDeserialize ( _err) => "json deserialization error" . to_string ( ) ,
118+ OpenAIError :: InvalidArgument ( _) => "invalid argument" . to_string ( ) ,
119+ OpenAIError :: FileSaveError ( _) | OpenAIError :: FileReadError ( _) => {
120+ "io error" . to_string ( )
121+ }
122+ OpenAIError :: ApiError ( e) => format ! ( "api error {:?}" , e) ,
123+ } ;
124+
125+ return Err ( message. into ( ) ) ;
126+ }
127+ } ;
110128
111129 trace ! ( "write to stdout, and finish the process" ) ;
112130 writeln ! ( std:: io:: stdout( ) , "{}" , result) ?;
@@ -123,36 +141,36 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
123141
124142 // directly commit the changes to the repository if the --commit option is enabled
125143 if cli. commit {
126- trace ! ( "Commit option is enabled, will commit the changes to the repository" ) ;
144+ trace ! ( "commit option is enabled, will commit the changes to the repository" ) ;
127145 let mut confirm = Confirm :: new ( ) ;
128146 confirm
129- . with_prompt ( "Do you want to commit the changes with the generated commit message?" )
147+ . with_prompt ( "do you want to commit the changes with the generated commit message?" )
130148 . default ( false ) ;
131149
132150 // Prompt the user for confirmation if --yes option is not enabled
133151 if cli. yes || confirm. interact ( ) ? {
134152 match repository. commit ( & result) {
135153 Ok ( _) => {
136- writeln ! ( std:: io:: stdout( ) , "Commit successful!" ) ?;
154+ writeln ! ( std:: io:: stdout( ) , "commit successful!" ) ?;
137155 }
138156 Err ( e) => {
139- writeln ! ( std:: io:: stderr( ) , "Commit failed: {}" , e) ?;
157+ writeln ! ( std:: io:: stderr( ) , "commit failed: {}" , e) ?;
140158 }
141159 }
142160 }
143161 }
144162
145163 // If the --save option is enabled, save the commit message to a file
146164 if !cli. save . is_empty ( ) {
147- trace ! ( "Save option is enabled, will save the commit message to a file" ) ;
165+ trace ! ( "save option is enabled, will save the commit message to a file" ) ;
148166 let save_path = & cli. save ;
149- debug ! ( "The save file path is {:?}" , & save_path) ;
167+ debug ! ( "the save file path is {:?}" , & save_path) ;
150168
151169 let mut file = File :: create ( save_path) ?;
152170 file. write_all ( result. as_bytes ( ) ) ?;
153171 file. flush ( ) ?;
154172
155- writeln ! ( std:: io:: stdout( ) , "Commit message saved to {}" , & save_path) ?;
173+ writeln ! ( std:: io:: stdout( ) , "commit message saved to {}" , & save_path) ?;
156174 }
157175
158176 Ok ( ( ) )
0 commit comments