@@ -7,7 +7,7 @@ pub enum OpenAIError {
7
7
#[ error( "http error: {0}" ) ]
8
8
Reqwest ( #[ from] reqwest:: Error ) ,
9
9
/// OpenAI returns error object with details of API call failure
10
- #[ error( "{:?}: {}" , . 0 . r#type , . 0 . message ) ]
10
+ #[ error( "{0}" ) ]
11
11
ApiError ( ApiError ) ,
12
12
/// Error when a response cannot be deserialized into a Rust type
13
13
#[ error( "failed to deserialize api response: {0}" ) ]
@@ -36,6 +36,31 @@ pub struct ApiError {
36
36
pub code : Option < String > ,
37
37
}
38
38
39
+ impl std:: fmt:: Display for ApiError {
40
+ /// If all fields are available, `ApiError` is formatted as:
41
+ /// `{type}: {message} (param: {param}) (code: {code})`
42
+ /// Otherwise, missing fields will be ignored.
43
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
44
+ let mut parts = Vec :: new ( ) ;
45
+
46
+ if let Some ( r#type) = & self . r#type {
47
+ parts. push ( format ! ( "{}:" , r#type) ) ;
48
+ }
49
+
50
+ parts. push ( self . message . clone ( ) ) ;
51
+
52
+ if let Some ( param) = & self . param {
53
+ parts. push ( format ! ( "(param: {param})" ) ) ;
54
+ }
55
+
56
+ if let Some ( code) = & self . code {
57
+ parts. push ( format ! ( "(code: {code})" ) ) ;
58
+ }
59
+
60
+ write ! ( f, "{}" , parts. join( " " ) )
61
+ }
62
+ }
63
+
39
64
/// Wrapper to deserialize the error object nested in "error" JSON key
40
65
#[ derive( Debug , Deserialize ) ]
41
66
pub ( crate ) struct WrappedError {
0 commit comments