Skip to main content

novel_openai/
error.rs

1//! Errors originating from API calls, parsing responses, and reading-or-writing to the file system.
2use serde::{Deserialize, Serialize};
3
4#[cfg(all(feature = "_api", not(target_family = "wasm")))]
5#[derive(Debug, thiserror::Error)]
6pub enum OpenAIError {
7    /// Underlying error from reqwest library after an API call was made
8    #[error("http error: {0}")]
9    Reqwest(#[from] reqwest::Error),
10    /// OpenAI returns error object with details of API call failure
11    #[error("{0}")]
12    ApiError(ApiError),
13    /// Error when a response cannot be deserialized into a Rust type
14    #[error("failed to deserialize api response: error:{0} content:{1}")]
15    JsonDeserialize(serde_json::Error, String),
16    /// Error on the client side when saving file to file system
17    #[error("failed to save file: {0}")]
18    FileSave(String),
19    /// Error on the client side when reading file from file system
20    #[error("failed to read file: {0}")]
21    FileRead(String),
22    /// Error on SSE streaming
23    #[error("stream failed: {0}")]
24    Stream(String),
25    /// Error from client side validation
26    /// or when builder fails to build request before making API call
27    #[error("invalid args: {0}")]
28    InvalidArgument(String),
29}
30
31// no streaming support for wasm yet
32#[cfg(all(feature = "_api", target_family = "wasm"))]
33#[derive(Debug, thiserror::Error)]
34pub enum OpenAIError {
35    /// Underlying error from reqwest library after an API call was made
36    #[error("http error: {0}")]
37    Reqwest(#[from] reqwest::Error),
38    /// OpenAI returns error object with details of API call failure
39    #[error("{0}")]
40    ApiError(ApiError),
41    /// Error when a response cannot be deserialized into a Rust type
42    #[error("failed to deserialize api response: error:{0} content:{1}")]
43    JsonDeserialize(serde_json::Error, String),
44    /// Error from client side validation
45    /// or when builder fails to build request before making API call
46    #[error("invalid args: {0}")]
47    InvalidArgument(String),
48}
49
50#[cfg(not(feature = "_api"))]
51#[derive(Debug)]
52pub enum OpenAIError {
53    /// Error from client side validation
54    /// or when builder fails to build request before making API call
55    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(all(feature = "_api", not(target_family = "wasm")))]
71#[derive(Debug, thiserror::Error)]
72pub enum StreamError {
73    /// Underlying error from reqwest_eventsource library when reading the stream
74    #[error("{0}")]
75    ReqwestEventSource(#[from] reqwest_eventsource::Error),
76    /// Error when a stream event does not match one of the expected values
77    #[error("Unknown event: {0:#?}")]
78    UnknownEvent(eventsource_stream::Event),
79    /// Error from eventsource_stream when parsing SSE
80    #[error("EventStream error: {0}")]
81    EventStream(String),
82}
83
84/// OpenAI API returns error object on failure
85#[derive(Debug, Serialize, Deserialize, Clone)]
86pub struct ApiError {
87    pub message: String,
88    #[serde(rename = "type")]
89    pub kind: Option<String>,
90    pub param: Option<String>,
91    pub code: Option<String>,
92}
93
94impl std::fmt::Display for ApiError {
95    /// If all fields are available, `ApiError` is formatted as:
96    /// `{type}: {message} (param: {param}) (code: {code})`
97    /// Otherwise, missing fields will be ignored.
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        let mut parts = Vec::new();
100
101        if let Some(kind) = &self.kind {
102            parts.push(format!("{kind}:"));
103        }
104
105        parts.push(self.message.clone());
106
107        if let Some(param) = &self.param {
108            parts.push(format!("(param: {param})"));
109        }
110
111        if let Some(code) = &self.code {
112            parts.push(format!("(code: {code})"));
113        }
114
115        write!(f, "{}", parts.join(" "))
116    }
117}
118
119/// Wrapper to deserialize the error object nested in "error" JSON key
120#[derive(Debug, Deserialize, Serialize)]
121pub struct WrappedError {
122    pub error: ApiError,
123}
124
125#[cfg(feature = "_api")]
126pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
127    let json_content = String::from_utf8_lossy(bytes);
128    tracing::error!("failed deserialization of: {}", json_content);
129    OpenAIError::JsonDeserialize(e, json_content.to_string())
130}