@@ -6,9 +6,12 @@ use reqwest::{multipart::Form, Response};
66use reqwest_eventsource:: { Error as EventSourceError , Event , EventSource , RequestBuilderExt } ;
77use serde:: { de:: DeserializeOwned , Serialize } ;
88
9+ #[ cfg( not( feature = "string-errors" ) ) ]
10+ use crate :: error:: { ApiError , WrappedError } ;
11+
912use crate :: {
1013 config:: { Config , OpenAIConfig } ,
11- error:: { map_deserialization_error, ApiError , OpenAIError , StreamError , WrappedError } ,
14+ error:: { map_deserialization_error, OpenAIError , StreamError } ,
1215 file:: Files ,
1316 image:: Images ,
1417 moderation:: Moderations ,
@@ -366,6 +369,7 @@ impl<C: Config> Client<C> {
366369 Ok ( bytes) => Ok ( bytes) ,
367370 Err ( e) => {
368371 match e {
372+ #[ cfg( not( feature = "string-errors" ) ) ]
369373 OpenAIError :: ApiError ( api_error) => {
370374 if status. is_server_error ( ) {
371375 Err ( backoff:: Error :: Transient {
@@ -385,6 +389,17 @@ impl<C: Config> Client<C> {
385389 Err ( backoff:: Error :: Permanent ( OpenAIError :: ApiError ( api_error) ) )
386390 }
387391 }
392+ #[ cfg( feature = "string-errors" ) ]
393+ OpenAIError :: ApiError ( api_error) => {
394+ if status. is_server_error ( ) {
395+ Err ( backoff:: Error :: Transient {
396+ err : OpenAIError :: ApiError ( api_error) ,
397+ retry_after : None ,
398+ } )
399+ } else {
400+ Err ( backoff:: Error :: Permanent ( OpenAIError :: ApiError ( api_error) ) )
401+ }
402+ }
388403 _ => Err ( backoff:: Error :: Permanent ( e) ) ,
389404 }
390405 }
@@ -483,6 +498,7 @@ async fn read_response(response: Response) -> Result<Bytes, OpenAIError> {
483498 let status = response. status ( ) ;
484499 let bytes = response. bytes ( ) . await . map_err ( OpenAIError :: Reqwest ) ?;
485500
501+ #[ cfg( not( feature = "string-errors" ) ) ]
486502 if status. is_server_error ( ) {
487503 // OpenAI does not guarantee server errors are returned as JSON so we cannot deserialize them.
488504 let message: String = String :: from_utf8_lossy ( & bytes) . into_owned ( ) ;
@@ -497,10 +513,18 @@ async fn read_response(response: Response) -> Result<Bytes, OpenAIError> {
497513
498514 // Deserialize response body from either error object or actual response object
499515 if !status. is_success ( ) {
500- let wrapped_error: WrappedError = serde_json:: from_slice ( bytes. as_ref ( ) )
501- . map_err ( |e| map_deserialization_error ( e, bytes. as_ref ( ) ) ) ?;
516+ #[ cfg( not( feature = "string-errors" ) ) ]
517+ {
518+ let wrapped_error: WrappedError = serde_json:: from_slice ( bytes. as_ref ( ) )
519+ . map_err ( |e| map_deserialization_error ( e, bytes. as_ref ( ) ) ) ?;
520+ return Err ( OpenAIError :: ApiError ( wrapped_error. error ) ) ;
521+ }
502522
503- return Err ( OpenAIError :: ApiError ( wrapped_error. error ) ) ;
523+ #[ cfg( feature = "string-errors" ) ]
524+ {
525+ let message: String = String :: from_utf8_lossy ( & bytes) . into_owned ( ) ;
526+ return Err ( OpenAIError :: ApiError ( message) ) ;
527+ }
504528 }
505529
506530 Ok ( bytes)
0 commit comments