Skip to content

Commit 0b84996

Browse files
authored
retain useful information in error messages for debuggability (#62)
1 parent f785d85 commit 0b84996

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

async-openai/src/client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::{de::DeserializeOwned, Serialize};
77

88
use crate::{
99
edit::Edits,
10-
error::{OpenAIError, WrappedError},
10+
error::{map_deserialization_error, OpenAIError, WrappedError},
1111
file::Files,
1212
image::Images,
1313
moderation::Moderations,
@@ -226,8 +226,8 @@ impl Client {
226226
let bytes = response.bytes().await?;
227227

228228
if !status.is_success() {
229-
let wrapped_error: WrappedError =
230-
serde_json::from_slice(bytes.as_ref()).map_err(OpenAIError::JSONDeserialize)?;
229+
let wrapped_error: WrappedError = serde_json::from_slice(bytes.as_ref())
230+
.map_err(|e| map_deserialization_error(e, bytes.as_ref()))?;
231231

232232
return Err(OpenAIError::ApiError(wrapped_error.error));
233233
}
@@ -264,7 +264,7 @@ impl Client {
264264
// Deserialize response body from either error object or actual response object
265265
if !status.is_success() {
266266
let wrapped_error: WrappedError = serde_json::from_slice(bytes.as_ref())
267-
.map_err(OpenAIError::JSONDeserialize)
267+
.map_err(|e| map_deserialization_error(e, bytes.as_ref()))
268268
.map_err(backoff::Error::Permanent)?;
269269

270270
if status.as_u16() == 429
@@ -286,7 +286,7 @@ impl Client {
286286
}
287287

288288
let response: O = serde_json::from_slice(bytes.as_ref())
289-
.map_err(OpenAIError::JSONDeserialize)
289+
.map_err(|e| map_deserialization_error(e, bytes.as_ref()))
290290
.map_err(backoff::Error::Permanent)?;
291291
Ok(response)
292292
})

async-openai/src/download.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,28 @@ pub(crate) async fn download_url<P: AsRef<Path>>(
3333

3434
if !response.status().is_success() {
3535
return Err(OpenAIError::FileSaveError(format!(
36-
"couldn't download file (status: {})",
36+
"couldn't download file, status: {}, url: {url}",
3737
response.status()
3838
)));
3939
}
4040

4141
let (dir, file_path) = create_paths(&parsed_url, dir);
4242

43-
tokio::fs::create_dir_all(dir)
43+
tokio::fs::create_dir_all(dir.as_path())
4444
.await
45-
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
45+
.map_err(|e| {
46+
OpenAIError::FileSaveError(format!("{}, dir: {}", e.to_string(), dir.display()))
47+
})?;
4648

4749
tokio::fs::write(
4850
file_path.as_path(),
49-
response
50-
.bytes()
51-
.await
52-
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?,
51+
response.bytes().await.map_err(|e| {
52+
OpenAIError::FileSaveError(format!(
53+
"{}, file path: {}",
54+
e.to_string(),
55+
file_path.display()
56+
))
57+
})?,
5358
)
5459
.await
5560
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
@@ -75,7 +80,9 @@ pub(crate) async fn save_b64<P: AsRef<Path>>(b64: &str, dir: P) -> Result<PathBu
7580
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?,
7681
)
7782
.await
78-
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
83+
.map_err(|e| {
84+
OpenAIError::FileSaveError(format!("{}, path: {}", e.to_string(), path.display()))
85+
})?;
7986

8087
Ok(path)
8188
}

async-openai/src/error.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum OpenAIError {
1818
/// Error on the client side when reading file from file system
1919
#[error("failed to read file: {0}")]
2020
FileReadError(String),
21-
/// Error when trying to stream completions SSE
21+
/// Error on SSE streaming
2222
#[error("stream failed: {0}")]
2323
StreamError(String),
2424
/// Error from client side validation
@@ -41,3 +41,11 @@ pub struct ApiError {
4141
pub(crate) struct WrappedError {
4242
pub(crate) error: ApiError,
4343
}
44+
45+
pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
46+
tracing::error!(
47+
"failed deserialization of: {}",
48+
String::from_utf8_lossy(bytes.as_ref())
49+
);
50+
OpenAIError::JSONDeserialize(e)
51+
}

0 commit comments

Comments
 (0)