1use serde::{Deserialize, Serialize};
4
5#[cfg(feature = "_api")]
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 #[cfg(all(feature = "_api", not(target_family = "wasm")))]
18 #[error("failed to save file: {0}")]
20 FileSaveError(String),
21 #[cfg(all(feature = "_api", not(target_family = "wasm")))]
22 #[error("failed to read file: {0}")]
24 FileReadError(String),
25 #[error("stream failed: {0}")]
27 StreamError(Box<StreamError>),
28 #[cfg(feature = "middleware")]
30 #[error(transparent)]
31 Boxed(Box<dyn std::error::Error + Send + Sync + 'static>),
32 #[error("invalid args: {0}")]
35 InvalidArgument(String),
36}
37
38#[cfg(all(feature = "_api", feature = "middleware"))]
39impl From<tower::BoxError> for OpenAIError {
40 fn from(error: tower::BoxError) -> Self {
41 OpenAIError::Boxed(error)
42 }
43}
44
45#[cfg(not(feature = "_api"))]
46#[derive(Debug)]
47pub enum OpenAIError {
48 InvalidArgument(String),
51}
52
53#[cfg(not(feature = "_api"))]
54impl std::fmt::Display for OpenAIError {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 match self {
57 OpenAIError::InvalidArgument(msg) => write!(f, "invalid args: {}", msg),
58 }
59 }
60}
61
62#[cfg(not(feature = "_api"))]
63impl std::error::Error for OpenAIError {}
64
65#[cfg(feature = "_api")]
66#[derive(Debug, thiserror::Error)]
67pub enum StreamError {
68 #[error("Unknown event: {0:#?}")]
70 UnknownEvent(eventsource_stream::Event),
71 #[error("EventStream error: {0}")]
73 EventStream(String),
74}
75
76#[derive(Debug, Serialize, Deserialize, Clone)]
78pub struct ApiError {
79 pub message: String,
80 pub r#type: Option<String>,
81 pub param: Option<String>,
82 pub code: Option<String>,
83}
84
85impl std::fmt::Display for ApiError {
86 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 let mut parts = Vec::new();
91
92 if let Some(r#type) = &self.r#type {
93 parts.push(format!("{}:", r#type));
94 }
95
96 parts.push(self.message.clone());
97
98 if let Some(param) = &self.param {
99 parts.push(format!("(param: {param})"));
100 }
101
102 if let Some(code) = &self.code {
103 parts.push(format!("(code: {code})"));
104 }
105
106 write!(f, "{}", parts.join(" "))
107 }
108}
109
110impl std::error::Error for ApiError {}
111
112#[derive(Debug, Deserialize, Serialize)]
114pub struct WrappedError {
115 pub error: ApiError,
116}
117
118#[cfg(feature = "_api")]
119pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
120 let json_content = String::from_utf8_lossy(bytes);
121 tracing::error!("failed deserialization of: {}", json_content);
122
123 OpenAIError::JSONDeserialize(e, json_content.to_string())
124}