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 client side validation
27    /// or when builder fails to build request before making API call
28    #[error("invalid args: {0}")]
29    InvalidArgument(String),
30}
31
32// no streaming support for wasm yet
33#[cfg(all(feature = "_api", target_family = "wasm"))]
34#[derive(Debug, thiserror::Error)]
35pub enum OpenAIError {
36    /// Underlying error from reqwest library after an API call was made
37    #[error("http error: {0}")]
38    Reqwest(#[from] reqwest::Error),
39    /// OpenAI returns error object with details of API call failure
40    #[error("{0}")]
41    ApiError(ApiError),
42    /// Error when a response cannot be deserialized into a Rust type
43    #[error("failed to deserialize api response: error:{0} content:{1}")]
44    JSONDeserialize(serde_json::Error, String),
45    /// Error from client side validation
46    /// or when builder fails to build request before making API call
47    #[error("invalid args: {0}")]
48    InvalidArgument(String),
49}
50
51#[cfg(not(feature = "_api"))]
52#[derive(Debug)]
53pub enum OpenAIError {
54    /// Error from client side validation
55    /// or when builder fails to build request before making API call
56    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 when a stream event does not match one of the expected values
75    #[error("Unknown event: {0:#?}")]
76    UnknownEvent(eventsource_stream::Event),
77    /// Error from eventsource_stream when parsing SSE
78    #[error("EventStream error: {0}")]
79    EventStream(String),
80}
81
82/// OpenAI API returns error object on failure
83#[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    /// If all fields are available, `ApiError` is formatted as:
93    /// `{type}: {message} (param: {param}) (code: {code})`
94    /// Otherwise, missing fields will be ignored.
95    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/// Wrapper to deserialize the error object nested in "error" JSON key
119#[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}