use std::fmt::Display; use tokio_util::codec::LinesCodecError; use crate::types; pub type Result = std::result::Result; #[derive(Debug)] pub enum Error { Env(std::env::VarError), HttpClient(reqwest::Error), Token(gcp_auth::Error), Serde(serde_json::Error), VertexError(types::VertexApiError), NoCandidatesError, /// An error occurred while decoding the SSE event stream. EventSourceError(LinesCodecError), /// An HTTP error status was returned by the server. HttpStatus { status: reqwest::StatusCode, body: String, }, } impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self { Error::Env(e) => write!(f, "Environment variable error: {e}"), Error::HttpClient(e) => write!(f, "HTTP Client error: {e}"), Error::Token(e) => write!(f, "Token error: {e}"), Error::Serde(e) => write!(f, "Serde error: {e}"), Error::VertexError(e) => write!(f, "Vertex error: {e}"), Error::NoCandidatesError => write!(f, "No candidates returned for the prompt"), Error::EventSourceError(e) => write!(f, "EventSource error: {e}"), Error::HttpStatus { status, body } => write!(f, "HTTP status {status}: {body}"), } } } impl std::error::Error for Error {} impl From for Error { fn from(e: reqwest::Error) -> Self { Error::HttpClient(e) } } impl From for Error { fn from(e: std::env::VarError) -> Self { Error::Env(e) } } impl From for Error { fn from(e: serde_json::Error) -> Self { Error::Serde(e) } } impl From for Error { fn from(e: gcp_auth::Error) -> Self { Error::Token(e) } } impl From for Error { fn from(e: types::VertexApiError) -> Self { Error::VertexError(e) } } impl From for Error { fn from(e: LinesCodecError) -> Self { Error::EventSourceError(e) } }