@@ -8,9 +8,10 @@ pub enum OpenAIError {
88 /// Underlying error from reqwest library after an API call was made
99 #[ error( "http error: {0}" ) ]
1010 Reqwest ( #[ from] reqwest:: Error ) ,
11- /// OpenAI returns error object with details of API call failure
11+ /// OpenAI returns error object with details of API call failure, along
12+ /// with the HTTP status code from the response.
1213 #[ error( "{0}" ) ]
13- ApiError ( ApiError ) ,
14+ ApiError ( ApiErrorResponse ) ,
1415 /// Error when a response cannot be deserialized into a Rust type
1516 #[ error( "failed to deserialize api response: error:{0} content:{1}" ) ]
1617 JSONDeserialize ( serde_json:: Error , String ) ,
@@ -109,6 +110,26 @@ impl std::fmt::Display for ApiError {
109110
110111impl std:: error:: Error for ApiError { }
111112
113+ /// `ApiError` paired with the HTTP status code from the response.
114+ #[ cfg( feature = "_api" ) ]
115+ #[ derive( Debug , Clone ) ]
116+ pub struct ApiErrorResponse {
117+ /// HTTP status code
118+ pub status_code : reqwest:: StatusCode ,
119+ /// Parsed error from response
120+ pub api_error : ApiError ,
121+ }
122+
123+ #[ cfg( feature = "_api" ) ]
124+ impl std:: fmt:: Display for ApiErrorResponse {
125+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
126+ write ! ( f, "{} {}" , self . status_code, self . api_error)
127+ }
128+ }
129+
130+ #[ cfg( feature = "_api" ) ]
131+ impl std:: error:: Error for ApiErrorResponse { }
132+
112133/// Wrapper to deserialize the error object nested in "error" JSON key
113134#[ derive( Debug , Deserialize , Serialize ) ]
114135pub struct WrappedError {
0 commit comments