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 #[cfg(feature = "middleware")]
28 #[error(transparent)]
29 Boxed(Box<dyn std::error::Error + Send + Sync + 'static>),
30 #[error("invalid args: {0}")]
33 InvalidArgument(String),
34}
35
36#[cfg(all(feature = "_api", feature = "middleware"))]
37impl From<tower::BoxError> for OpenAIError {
38 fn from(error: tower::BoxError) -> Self {
39 OpenAIError::Boxed(error)
40 }
41}
42
43#[cfg(all(feature = "_api", target_family = "wasm"))]
45#[derive(Debug, thiserror::Error)]
46pub enum OpenAIError {
47 #[error("http error: {0}")]
49 Reqwest(#[from] reqwest::Error),
50 #[error("{0}")]
52 ApiError(ApiError),
53 #[error("failed to deserialize api response: error:{0} content:{1}")]
55 JSONDeserialize(serde_json::Error, String),
56 #[cfg(feature = "middleware")]
58 #[error(transparent)]
59 Boxed(Box<dyn std::error::Error + 'static>),
60 #[error("invalid args: {0}")]
63 InvalidArgument(String),
64}
65
66#[cfg(not(feature = "_api"))]
67#[derive(Debug)]
68pub enum OpenAIError {
69 InvalidArgument(String),
72}
73
74#[cfg(not(feature = "_api"))]
75impl std::fmt::Display for OpenAIError {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 match self {
78 OpenAIError::InvalidArgument(msg) => write!(f, "invalid args: {}", msg),
79 }
80 }
81}
82
83#[cfg(not(feature = "_api"))]
84impl std::error::Error for OpenAIError {}
85
86#[cfg(all(feature = "_api", not(target_family = "wasm")))]
87#[derive(Debug, thiserror::Error)]
88pub enum StreamError {
89 #[error("Unknown event: {0:#?}")]
91 UnknownEvent(eventsource_stream::Event),
92 #[error("EventStream error: {0}")]
94 EventStream(String),
95}
96
97#[derive(Debug, Serialize, Deserialize, Clone)]
99pub struct ApiError {
100 pub message: String,
101 pub r#type: Option<String>,
102 pub param: Option<String>,
103 pub code: Option<String>,
104}
105
106impl std::fmt::Display for ApiError {
107 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 let mut parts = Vec::new();
112
113 if let Some(r#type) = &self.r#type {
114 parts.push(format!("{}:", r#type));
115 }
116
117 parts.push(self.message.clone());
118
119 if let Some(param) = &self.param {
120 parts.push(format!("(param: {param})"));
121 }
122
123 if let Some(code) = &self.code {
124 parts.push(format!("(code: {code})"));
125 }
126
127 write!(f, "{}", parts.join(" "))
128 }
129}
130
131impl std::error::Error for ApiError {}
132
133#[derive(Debug, Deserialize, Serialize)]
135pub struct WrappedError {
136 pub error: ApiError,
137}
138
139#[cfg(feature = "_api")]
140pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
141 let json_content = String::from_utf8_lossy(bytes);
142 tracing::error!("failed deserialization of: {}", json_content);
143
144 OpenAIError::JSONDeserialize(e, json_content.to_string())
145}