Skip to main content

async_openai/
error.rs

1//! Errors originating from API calls, parsing responses, and reading-or-writing to the file system.
2
3use serde::{Deserialize, Serialize};
4
5#[cfg(all(feature = "_api", not(target_family = "wasm")))]
6#[derive(Debug, thiserror::Error)]
7pub enum OpenAIError {
8    /// Underlying error from reqwest library after an API call was made
9    #[error("http error: {0}")]
10    Reqwest(#[from] reqwest::Error),
11    /// OpenAI returns error object with details of API call failure
12    #[error("{0}")]
13    ApiError(ApiError),
14    /// Error when a response cannot be deserialized into a Rust type
15    #[error("failed to deserialize api response: error:{0} content:{1}")]
16    JSONDeserialize(serde_json::Error, String),
17    /// Error on the client side when saving file to file system
18    #[error("failed to save file: {0}")]
19    FileSaveError(String),
20    /// Error on the client side when reading file from file system
21    #[error("failed to read file: {0}")]
22    FileReadError(String),
23    /// Error on SSE streaming
24    #[error("stream failed: {0}")]
25    StreamError(Box<StreamError>),
26    /// Error from middlewares
27    #[cfg(feature = "middleware")]
28    #[error(transparent)]
29    Boxed(Box<dyn std::error::Error + Send + Sync + 'static>),
30    /// Error from client side validation
31    /// or when builder fails to build request before making API call
32    #[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// no streaming support for wasm yet
44#[cfg(all(feature = "_api", target_family = "wasm"))]
45#[derive(Debug, thiserror::Error)]
46pub enum OpenAIError {
47    /// Underlying error from reqwest library after an API call was made
48    #[error("http error: {0}")]
49    Reqwest(#[from] reqwest::Error),
50    /// OpenAI returns error object with details of API call failure
51    #[error("{0}")]
52    ApiError(ApiError),
53    /// Error when a response cannot be deserialized into a Rust type
54    #[error("failed to deserialize api response: error:{0} content:{1}")]
55    JSONDeserialize(serde_json::Error, String),
56    /// Error from middlewares
57    #[cfg(feature = "middleware")]
58    #[error(transparent)]
59    Boxed(Box<dyn std::error::Error + 'static>),
60    /// Error from client side validation
61    /// or when builder fails to build request before making API call
62    #[error("invalid args: {0}")]
63    InvalidArgument(String),
64}
65
66#[cfg(not(feature = "_api"))]
67#[derive(Debug)]
68pub enum OpenAIError {
69    /// Error from client side validation
70    /// or when builder fails to build request before making API call
71    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 when a stream event does not match one of the expected values
90    #[error("Unknown event: {0:#?}")]
91    UnknownEvent(eventsource_stream::Event),
92    /// Error from eventsource_stream when parsing SSE
93    #[error("EventStream error: {0}")]
94    EventStream(String),
95}
96
97/// OpenAI API returns error object on failure
98#[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    /// If all fields are available, `ApiError` is formatted as:
108    /// `{type}: {message} (param: {param}) (code: {code})`
109    /// Otherwise, missing fields will be ignored.
110    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/// Wrapper to deserialize the error object nested in "error" JSON key
134#[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}