async_openai_wasm/
error.rs1use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "_api")]
5#[derive(Debug, thiserror::Error)]
6pub enum OpenAIError {
7 #[error("http error: {0}")]
9 Reqwest(#[from] reqwest::Error),
10 #[error("{0}")]
12 ApiError(ApiError),
13 #[error("failed to deserialize api response: error:{0} content:{1}")]
15 JSONDeserialize(serde_json::Error, String),
16 #[error("failed to save file: {0}")]
18 FileSaveError(String),
19 #[error("failed to read file: {0}")]
21 FileReadError(String),
22 #[error("stream failed: {0}")]
24 StreamError(Box<StreamError>),
25 #[error("invalid args: {0}")]
28 InvalidArgument(String),
29}
30
31#[cfg(not(feature = "_api"))]
32#[derive(Debug)]
33pub enum OpenAIError {
34 InvalidArgument(String),
37}
38
39#[cfg(not(feature = "_api"))]
40impl std::fmt::Display for OpenAIError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 OpenAIError::InvalidArgument(msg) => write!(f, "invalid args: {}", msg),
44 }
45 }
46}
47
48#[cfg(not(feature = "_api"))]
49impl std::error::Error for OpenAIError {}
50
51#[cfg(feature = "_api")]
52#[derive(Debug, thiserror::Error)]
53pub enum StreamError {
54 #[error("{0}")]
56 ReqwestEventSource(#[from] reqwest_eventsource::Error),
57 #[error("Unknown event: {0:#?}")]
59 UnknownEvent(eventsource_stream::Event),
60 #[error("EventStream error: {0}")]
62 EventStream(String),
63}
64
65#[derive(Debug, Serialize, Deserialize, Clone)]
67pub struct ApiError {
68 pub message: String,
69 pub r#type: Option<String>,
70 pub param: Option<String>,
71 pub code: Option<String>,
72}
73
74impl std::fmt::Display for ApiError {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 let mut parts = Vec::new();
80
81 if let Some(r#type) = &self.r#type {
82 parts.push(format!("{}:", r#type));
83 }
84
85 parts.push(self.message.clone());
86
87 if let Some(param) = &self.param {
88 parts.push(format!("(param: {param})"));
89 }
90
91 if let Some(code) = &self.code {
92 parts.push(format!("(code: {code})"));
93 }
94
95 write!(f, "{}", parts.join(" "))
96 }
97}
98
99#[derive(Debug, Deserialize, Serialize)]
101pub struct WrappedError {
102 pub error: ApiError,
103}
104
105#[cfg(feature = "_api")]
106pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
107 let json_content = String::from_utf8_lossy(bytes);
108 tracing::error!("failed deserialization of: {}", json_content);
109
110 OpenAIError::JSONDeserialize(e, json_content.to_string())
111}