use std::fmt;
use redup_bootstrap_grpc::{Details, details};
use tonic::Status;
#[derive(Debug, Clone)]
pub struct ClientError(Box<Inner>);
#[derive(Debug, Clone)]
struct Inner {
status: Status,
details: Details,
}
impl ClientError {
pub fn code(&self) -> tonic::Code {
self.0.status.code()
}
pub fn message(&self) -> &str {
self.0.status.message()
}
pub fn reason(&self) -> Option<&str> {
self.0.details.reason.as_deref()
}
pub fn domain(&self) -> Option<&str> {
self.0.details.domain.as_deref()
}
pub fn path(&self) -> Option<&str> {
self.0.details.path.as_deref()
}
pub fn public_message(&self) -> Option<&str> {
self.0.details.public_message.as_deref()
}
pub fn debug(&self) -> Option<&str> {
self.0.details.debug.as_deref()
}
pub fn status(&self) -> &Status {
&self.0.status
}
pub fn into_status(self) -> Status {
self.0.status
}
pub fn into_error(self) -> redup_bootstrap_grpc::Error {
redup_bootstrap_grpc::Error::from_status(self.0.status)
}
pub(crate) fn from_status(status: Status) -> Self {
Self(Box::new(Inner {
details: details(&status),
status,
}))
}
}
impl From<Status> for ClientError {
fn from(status: Status) -> Self {
Self::from_status(status)
}
}
impl From<ClientError> for Status {
fn from(error: ClientError) -> Self {
error.0.status
}
}
impl From<ClientError> for redup_bootstrap_grpc::Error {
fn from(error: ClientError) -> Self {
error.into_error()
}
}
impl fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code_name(), self.0.status.message())
}
}
impl std::error::Error for ClientError {}
impl ClientError {
fn code_name(&self) -> &'static str {
code_name(self.0.status.code())
}
}
pub fn code_name(code: tonic::Code) -> &'static str {
match code {
tonic::Code::Ok => "OK",
tonic::Code::Cancelled => "Cancelled",
tonic::Code::Unknown => "Unknown",
tonic::Code::InvalidArgument => "InvalidArgument",
tonic::Code::DeadlineExceeded => "DeadlineExceeded",
tonic::Code::NotFound => "NotFound",
tonic::Code::AlreadyExists => "AlreadyExists",
tonic::Code::PermissionDenied => "PermissionDenied",
tonic::Code::ResourceExhausted => "ResourceExhausted",
tonic::Code::FailedPrecondition => "FailedPrecondition",
tonic::Code::Aborted => "Aborted",
tonic::Code::OutOfRange => "OutOfRange",
tonic::Code::Unimplemented => "Unimplemented",
tonic::Code::Internal => "Internal",
tonic::Code::Unavailable => "Unavailable",
tonic::Code::DataLoss => "DataLoss",
tonic::Code::Unauthenticated => "Unauthenticated",
}
}