Skip to main content

openai_oxide/
error.rs

1// OpenAI API error types
2
3use serde::{Deserialize, Serialize};
4
5/// Error response body from the OpenAI API.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ErrorResponse {
8    pub error: ApiErrorDetail,
9}
10
11/// Detail within an API error response.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ApiErrorDetail {
14    pub message: String,
15    #[serde(rename = "type")]
16    pub type_: Option<String>,
17    pub param: Option<String>,
18    pub code: Option<String>,
19}
20
21/// All errors that can occur when using this library.
22#[derive(Debug, thiserror::Error)]
23pub enum OpenAIError {
24    /// The API returned an error response.
25    #[error("API error (status {status}): {message}")]
26    ApiError {
27        status: u16,
28        message: String,
29        type_: Option<String>,
30        code: Option<String>,
31    },
32
33    /// HTTP request failed.
34    #[error("request error: {0}")]
35    RequestError(#[from] reqwest::Error),
36
37    /// JSON (de)serialization failed.
38    #[error("JSON error: {0}")]
39    JsonError(#[from] serde_json::Error),
40
41    /// SSE stream error.
42    #[error("stream error: {0}")]
43    StreamError(String),
44
45    /// Invalid argument passed by the caller.
46    #[error("invalid argument: {0}")]
47    InvalidArgument(String),
48}