use std::fmt::{self, Display, Formatter};
pub use super::api::HmcParseError;
pub use http::Error as HttpError;
pub use reqwest::Error as ReqwestError;
pub use tonic::{transport::Error as TransportError, Code, Status};
pub type ClientResult<T> = Result<T, ClientError>;
#[derive(Debug)]
pub enum ClientError {
Grpc(Status),
Transport(TransportError),
Reqwest(ReqwestError),
Http(HttpError),
NoAuthId,
Unauthenticated,
}
impl Display for ClientError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
ClientError::Grpc(status) => write!(f, "An error occured in the gRPC server or client: {}", status),
ClientError::Transport(transport_err) => write!(f, "And error occured in gRPC's transport layer: {}", transport_err),
ClientError::Reqwest(reqwest_err) => write!(f, "An error occured in HTTP client, or request was unsuccessful: {}", reqwest_err),
ClientError::Http(http_err) => write!(f, "An error occured while parsing an URL / creating an HTTP request: {}", http_err),
ClientError::NoAuthId => write!(f, "No authentication session is in progress, but client tries to call auth API methods that need it"),
ClientError::Unauthenticated => write!(f, "Client is not authenticated, but the API it tries to call requires authentication"),
}
}
}
impl From<Status> for ClientError {
fn from(e: Status) -> Self {
Self::Grpc(e)
}
}
impl From<TransportError> for ClientError {
fn from(e: TransportError) -> Self {
Self::Transport(e)
}
}
impl From<ReqwestError> for ClientError {
fn from(e: ReqwestError) -> Self {
Self::Reqwest(e)
}
}
impl From<HttpError> for ClientError {
fn from(e: HttpError) -> Self {
Self::Http(e)
}
}