openai_interface/
errors.rs1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Debug, Deserialize, Serialize, Clone)]
13pub struct ApiError {
14 #[serde(default)]
16 pub status: u16,
17 #[serde(default)]
19 pub message: String,
20 #[serde(rename = "type", alias = "error_type", default)]
22 pub error_type: Option<String>,
23 #[serde(default, deserialize_with = "deserialize_code")]
25 pub code: Option<String>,
26}
27
28fn deserialize_code<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
31where
32 D: serde::Deserializer<'de>,
33{
34 let value = Option::<serde_json::Value>::deserialize(deserializer)?;
35 Ok(match value {
36 None | Some(serde_json::Value::Null) => None,
37 Some(serde_json::Value::String(s)) => Some(s),
38 Some(other) => Some(other.to_string()),
39 })
40}
41
42impl std::fmt::Display for ApiError {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(f, "HTTP {}: {}", self.status, self.message)?;
45 if let Some(error_type) = &self.error_type {
46 write!(f, " (type: {error_type})")?;
47 }
48 if let Some(code) = &self.code {
49 write!(f, " (code: {code})")?;
50 }
51 Ok(())
52 }
53}
54
55impl std::error::Error for ApiError {}
56
57#[derive(Debug, Deserialize, Clone)]
59pub(crate) struct NestedApiError {
60 pub error: ApiError,
61}
62
63#[derive(Debug, Error)]
64pub enum OapiError {
65 #[error("Failed to send request: {0}")]
66 SendError(#[from] reqwest::Error),
67 #[error("Response error: {0}")]
68 ResponseError(String),
69 #[error("API request failed: {0}")]
70 ApiError(#[from] ApiError),
71 #[error("Failed to parse SSE stream: {0}")]
72 SseParseError(String),
73 #[error("You cannot post a streaming request in a non-streaming context")]
75 NonStreamingViolation,
76 #[error("You cannot post a non-streaming request in a streaming context")]
78 StreamingViolation,
79 #[error("Deserialization error:\n{0}\n\nPlease report this error in the project issue.")]
80 DeserializationError(String),
81 #[error("Invalid header: {0}")]
82 InvalidHeader(String),
83 #[error("File not found at: {0}")]
84 FileNotFoundError(PathBuf),
85 #[error("Failed to read file: {0}")]
86 FileReadError(#[from] std::io::Error),
87 #[error("Failed to operate on url: {0}")]
88 UrlError(#[from] url::ParseError),
89 #[error("Cannot be base: {0}")]
90 UrlCannotBeBase(String),
91}