async_openai_wasm/
error.rsuse serde::Deserialize;
#[derive(Debug, thiserror::Error)]
pub enum OpenAIError {
#[error("http error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("{0}")]
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, Clone)]
pub struct ApiError {
pub message: String,
pub r#type: Option<String>,
pub param: Option<String>,
pub code: Option<String>,
}
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut parts = Vec::new();
if let Some(r#type) = &self.r#type {
parts.push(format!("{}:", r#type));
}
parts.push(self.message.clone());
if let Some(param) = &self.param {
parts.push(format!("(param: {param})"));
}
if let Some(code) = &self.code {
parts.push(format!("(code: {code})"));
}
write!(f, "{}", parts.join(" "))
}
}
#[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)
}