1use serde::{Deserialize, Serialize};
4
5#[cfg(all(feature = "_api", not(target_family = "wasm")))]
6#[derive(Debug, thiserror::Error)]
7pub enum OpenAIError {
8 #[error("http error: {0}")]
10 Reqwest(#[from] reqwest::Error),
11 #[error("{0}")]
13 ApiError(ApiError),
14 #[error("failed to deserialize api response: error:{0} content:{1}")]
16 JSONDeserialize(serde_json::Error, String),
17 #[error("failed to save file: {0}")]
19 FileSaveError(String),
20 #[error("failed to read file: {0}")]
22 FileReadError(String),
23 #[error("stream failed: {0}")]
25 StreamError(Box<StreamError>),
26 #[error("invalid args: {0}")]
29 InvalidArgument(String),
30}
31
32#[cfg(all(feature = "_api", target_family = "wasm"))]
34#[derive(Debug, thiserror::Error)]
35pub enum OpenAIError {
36 #[error("http error: {0}")]
38 Reqwest(#[from] reqwest::Error),
39 #[error("{0}")]
41 ApiError(ApiError),
42 #[error("failed to deserialize api response: error:{0} content:{1}")]
44 JSONDeserialize(serde_json::Error, String),
45 #[error("invalid args: {0}")]
48 InvalidArgument(String),
49}
50
51#[cfg(not(feature = "_api"))]
52#[derive(Debug)]
53pub enum OpenAIError {
54 InvalidArgument(String),
57}
58
59#[cfg(not(feature = "_api"))]
60impl std::fmt::Display for OpenAIError {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 match self {
63 OpenAIError::InvalidArgument(msg) => write!(f, "invalid args: {}", msg),
64 }
65 }
66}
67
68#[cfg(not(feature = "_api"))]
69impl std::error::Error for OpenAIError {}
70
71#[cfg(all(feature = "_api", not(target_family = "wasm")))]
72#[derive(Debug, thiserror::Error)]
73pub enum StreamError {
74 #[error("{0}")]
76 ReqwestEventSource(#[from] reqwest_eventsource::Error),
77 #[error("Unknown event: {0:#?}")]
79 UnknownEvent(eventsource_stream::Event),
80 #[error("EventStream error: {0}")]
82 EventStream(String),
83}
84
85#[derive(Debug, Serialize, Deserialize, Clone)]
87pub struct ApiError {
88 pub message: String,
89 pub r#type: Option<String>,
90 pub param: Option<String>,
91 pub code: Option<String>,
92}
93
94impl std::fmt::Display for ApiError {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99 let mut parts = Vec::new();
100
101 if let Some(r#type) = &self.r#type {
102 parts.push(format!("{}:", r#type));
103 }
104
105 parts.push(self.message.clone());
106
107 if let Some(param) = &self.param {
108 parts.push(format!("(param: {param})"));
109 }
110
111 if let Some(code) = &self.code {
112 parts.push(format!("(code: {code})"));
113 }
114
115 write!(f, "{}", parts.join(" "))
116 }
117}
118
119#[derive(Debug, Deserialize, Serialize)]
121pub struct WrappedError {
122 pub error: ApiError,
123}
124
125#[cfg(feature = "_api")]
126pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
127 let json_content = String::from_utf8_lossy(bytes);
128 tracing::error!("failed deserialization of: {}", json_content);
129
130 OpenAIError::JSONDeserialize(e, json_content.to_string())
131}