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(feature = "_api")]
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    #[cfg(all(feature = "_api", not(target_family = "wasm")))]
18    /// Error on the client side when saving file to file system
19    #[error("failed to save file: {0}")]
20    FileSaveError(String),
21    #[cfg(all(feature = "_api", not(target_family = "wasm")))]
22    /// Error on the client side when reading file from file system
23    #[error("failed to read file: {0}")]
24    FileReadError(String),
25    /// Error on SSE streaming
26    #[error("stream failed: {0}")]
27    StreamError(Box<StreamError>),
28    /// Error from middlewares
29    #[cfg(feature = "middleware")]
30    #[error(transparent)]
31    Boxed(Box<dyn std::error::Error + Send + Sync + 'static>),
32    /// Error from client side validation
33    /// or when builder fails to build request before making API call
34    #[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    /// Error from client side validation
49    /// or when builder fails to build request before making API call
50    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 when a stream event does not match one of the expected values
69    #[error("Unknown event: {0:#?}")]
70    UnknownEvent(eventsource_stream::Event),
71    /// Error from eventsource_stream when parsing SSE
72    #[error("EventStream error: {0}")]
73    EventStream(String),
74}
75
76/// OpenAI API returns error object on failure
77#[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    /// If all fields are available, `ApiError` is formatted as:
87    /// `{type}: {message} (param: {param}) (code: {code})`
88    /// Otherwise, missing fields will be ignored.
89    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/// Wrapper to deserialize the error object nested in "error" JSON key
113#[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}