dynamo_async_openai/
error.rs1use serde::{Deserialize, Serialize};
13
14#[derive(Debug, thiserror::Error)]
15pub enum OpenAIError {
16 #[error("http error: {0}")]
18 Reqwest(#[from] reqwest::Error),
19 #[error("{0}")]
21 ApiError(ApiError),
22 #[error("failed to deserialize api response: {0}")]
24 JSONDeserialize(serde_json::Error),
25 #[error("failed to save file: {0}")]
27 FileSaveError(String),
28 #[error("failed to read file: {0}")]
30 FileReadError(String),
31 #[error("stream failed: {0}")]
33 StreamError(String),
34 #[error("invalid args: {0}")]
37 InvalidArgument(String),
38}
39
40#[derive(Debug, Serialize, Deserialize, Clone)]
42pub struct ApiError {
43 pub message: String,
44 pub r#type: Option<String>,
45 pub param: Option<String>,
46 pub code: Option<String>,
47}
48
49impl std::fmt::Display for ApiError {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 let mut parts = Vec::new();
55
56 if let Some(r#type) = &self.r#type {
57 parts.push(format!("{}:", r#type));
58 }
59
60 parts.push(self.message.clone());
61
62 if let Some(param) = &self.param {
63 parts.push(format!("(param: {param})"));
64 }
65
66 if let Some(code) = &self.code {
67 parts.push(format!("(code: {code})"));
68 }
69
70 write!(f, "{}", parts.join(" "))
71 }
72}
73
74#[derive(Debug, Deserialize, Serialize)]
76pub struct WrappedError {
77 pub error: ApiError,
78}
79
80pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
81 tracing::error!(
82 "failed deserialization of: {}",
83 String::from_utf8_lossy(bytes)
84 );
85 OpenAIError::JSONDeserialize(e)
86}