use serde::Deserialize;
#[derive(Debug, thiserror::Error)]
pub enum OpenAIError {
#[error("http error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("{:?}: {}", .0.r#type, .0.message)]
ApiError(ApiError),
#[error("failed to deserialize api response: {0}")]
JSONDeserialize(serde_json::Error),
#[error("failed to save file: {0}")]
FileSaveError(String),
#[error("failed to read file: {0}")]
FileReadError(String),
#[error("stream failed: {0}")]
StreamError(String),
#[error("invalid args: {0}")]
InvalidArgument(String),
}
#[derive(Debug, Deserialize)]
pub struct ApiError {
pub message: String,
pub r#type: Option<String>,
pub param: Option<String>,
pub code: Option<String>,
}
#[derive(Debug, Deserialize)]
pub(crate) struct WrappedError {
pub(crate) error: ApiError,
}
pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
tracing::error!(
"failed deserialization of: {}",
String::from_utf8_lossy(bytes)
);
OpenAIError::JSONDeserialize(e)
}