1use serde::{Deserialize, Serialize};
4
5pub use crate::types::shared::{
6 MisalignmentErrorDetailsResource, MisalignmentErrorType, MisalignmentSteer,
7};
8
9#[cfg(feature = "_api")]
10#[derive(Debug, thiserror::Error)]
11pub enum OpenAIError {
12 #[error("http error: {0}")]
14 Reqwest(#[from] reqwest::Error),
15 #[error("{0}")]
18 ApiError(ApiErrorResponse),
19 #[error("failed to deserialize api response: error:{0} content:{1}")]
21 JSONDeserialize(serde_json::Error, String),
22 #[cfg(all(feature = "_api", not(target_family = "wasm")))]
23 #[error("failed to save file: {0}")]
25 FileSaveError(String),
26 #[cfg(all(feature = "_api", not(target_family = "wasm")))]
27 #[error("failed to read file: {0}")]
29 FileReadError(String),
30 #[error("stream failed: {0}")]
32 StreamError(Box<StreamError>),
33 #[cfg(feature = "middleware")]
35 #[error(transparent)]
36 Boxed(Box<dyn std::error::Error + Send + Sync + 'static>),
37 #[error("invalid args: {0}")]
40 InvalidArgument(String),
41}
42
43#[cfg(all(feature = "_api", feature = "middleware"))]
44impl From<tower::BoxError> for OpenAIError {
45 fn from(error: tower::BoxError) -> Self {
46 OpenAIError::Boxed(error)
47 }
48}
49
50#[cfg(not(feature = "_api"))]
51#[derive(Debug)]
52pub enum OpenAIError {
53 InvalidArgument(String),
56}
57
58#[cfg(not(feature = "_api"))]
59impl std::fmt::Display for OpenAIError {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 match self {
62 OpenAIError::InvalidArgument(msg) => write!(f, "invalid args: {}", msg),
63 }
64 }
65}
66
67#[cfg(not(feature = "_api"))]
68impl std::error::Error for OpenAIError {}
69
70#[cfg(feature = "_api")]
71#[derive(Debug, thiserror::Error)]
72pub enum StreamError {
73 #[error("Unknown event: {0:#?}")]
75 UnknownEvent(eventsource_stream::Event),
76 #[error("EventStream error: {0}")]
78 EventStream(String),
79}
80
81#[derive(Debug, Serialize, Deserialize, Clone)]
83pub struct ApiError {
84 pub message: String,
85 pub r#type: Option<String>,
86 pub param: Option<String>,
87 pub code: Option<String>,
88 #[serde(skip_serializing_if = "Option::is_none")]
89 pub misalignment: Option<Box<MisalignmentErrorDetailsResource>>,
90}
91
92impl std::fmt::Display for ApiError {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 let mut parts = Vec::new();
98
99 if let Some(r#type) = &self.r#type {
100 parts.push(format!("{}:", r#type));
101 }
102
103 parts.push(self.message.clone());
104
105 if let Some(param) = &self.param {
106 parts.push(format!("(param: {param})"));
107 }
108
109 if let Some(code) = &self.code {
110 parts.push(format!("(code: {code})"));
111 }
112
113 write!(f, "{}", parts.join(" "))
114 }
115}
116
117impl std::error::Error for ApiError {}
118
119#[cfg(feature = "_api")]
121#[derive(Debug, Clone)]
122pub struct ApiErrorResponse {
123 pub status_code: reqwest::StatusCode,
125 pub api_error: ApiError,
127}
128
129#[cfg(feature = "_api")]
130impl std::fmt::Display for ApiErrorResponse {
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 write!(f, "{} {}", self.status_code, self.api_error)
133 }
134}
135
136#[cfg(feature = "_api")]
137impl std::error::Error for ApiErrorResponse {}
138
139#[derive(Debug, Deserialize, Serialize)]
141pub struct WrappedError {
142 pub error: ApiError,
143}
144
145#[cfg(feature = "_api")]
146pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
147 let json_content = String::from_utf8_lossy(bytes);
148 tracing::error!("failed deserialization of: {}", json_content);
149
150 OpenAIError::JSONDeserialize(e, json_content.to_string())
151}