1use crate::transport::ws::WsStream;
2use futures::stream::ReuniteError;
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5use tokio_tungstenite::tungstenite::protocol::Message;
6
7#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
8#[serde(rename_all = "snake_case")]
9pub enum ApiErrorType {
10 InvalidRequestError,
11 RateLimitError,
12 AuthenticationError,
13 ServerError,
14 #[serde(other)]
15 Unknown,
16}
17
18#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
19pub struct ServerError {
20 #[serde(rename = "type")]
21 pub error_type: ApiErrorType,
22 pub code: Option<String>,
23 pub message: String,
24 pub param: Option<String>,
26 pub event_id: Option<String>,
27}
28
29#[derive(Error, Debug)]
30pub enum Error {
31 #[error("WebSocket error: {0}")]
32 WebSocket(#[from] tokio_tungstenite::tungstenite::Error),
33
34 #[error("HTTP protocol error: {0}")]
35 Http(#[from] reqwest::Error),
36
37 #[error("Failed to parse or serialize JSON: {0}")]
38 Serialization(#[from] serde_json::Error),
39
40 #[error("Invalid URL: {0}")]
41 Url(#[from] url::ParseError),
42
43 #[error("Header error: {0}")]
44 Header(#[from] reqwest::header::InvalidHeaderValue),
45
46 #[error("IO error: {0}")]
47 Io(#[from] std::io::Error),
48
49 #[error("OpenAI API error: {0:?}")]
50 Api(ServerError),
51
52 #[error("The connection was closed unexpectedly")]
53 ConnectionClosed,
54
55 #[error("Failed to reunite split client: {0}")]
56 Reunite(#[from] ReuniteError<WsStream, Message>),
57
58 #[error("MIME type error: {0}")]
59 Mime(String),
60
61 #[error("Invalid client event: {0}")]
62 InvalidClientEvent(String),
63
64 #[error("Not implemented: {0}")]
65 NotImplemented(&'static str),
66}
67
68pub type Result<T> = std::result::Result<T, Error>;