use serde::{Deserialize, Serialize};
pub(crate) fn enum_to_string<T: Serialize>(value: &T) -> Result<String, OpenAIError> {
let v = serde_json::to_value(value)?;
v.as_str()
.map(|s| s.to_string())
.ok_or_else(|| OpenAIError::InvalidArgument(format!("expected string enum, got {v}")))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorResponse {
pub error: ApiErrorDetail,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiErrorDetail {
pub message: String,
#[serde(rename = "type")]
pub type_: Option<String>,
pub param: Option<String>,
pub code: Option<String>,
}
#[derive(Debug, thiserror::Error)]
pub enum OpenAIError {
#[error("API error (status {status}): {message}")]
ApiError {
status: u16,
message: String,
type_: Option<String>,
code: Option<String>,
request_id: Option<String>,
},
#[error("request error: {0}")]
RequestError(#[from] reqwest::Error),
#[error("JSON error: {0}")]
JsonError(#[from] serde_json::Error),
#[error("stream error: {0}")]
StreamError(String),
#[error("invalid argument: {0}")]
InvalidArgument(String),
#[cfg(feature = "websocket")]
#[error("websocket error: {0}")]
WebSocketError(String),
}