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("Unknown event: {0:#?}")]
76 UnknownEvent(eventsource_stream::Event),
77 #[error("EventStream error: {0}")]
79 EventStream(String),
80}
81
82#[derive(Debug, Serialize, Deserialize, Clone)]
84pub struct ApiError {
85 pub message: String,
86 pub r#type: Option<String>,
87 pub param: Option<String>,
88 pub code: Option<String>,
89}
90
91impl std::fmt::Display for ApiError {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 let mut parts = Vec::new();
97
98 if let Some(r#type) = &self.r#type {
99 parts.push(format!("{}:", r#type));
100 }
101
102 parts.push(self.message.clone());
103
104 if let Some(param) = &self.param {
105 parts.push(format!("(param: {param})"));
106 }
107
108 if let Some(code) = &self.code {
109 parts.push(format!("(code: {code})"));
110 }
111
112 write!(f, "{}", parts.join(" "))
113 }
114}
115
116impl std::error::Error for ApiError {}
117
118#[derive(Debug, Deserialize, Serialize)]
120pub struct WrappedError {
121 pub error: ApiError,
122}
123
124#[cfg(feature = "_api")]
125pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
126 let json_content = String::from_utf8_lossy(bytes);
127 tracing::error!("failed deserialization of: {}", json_content);
128
129 OpenAIError::JSONDeserialize(e, json_content.to_string())
130}