redup_bootstrap_client/
error.rs1use std::fmt;
4
5use redup_bootstrap_grpc::{Details, details};
6use tonic::Status;
7
8#[derive(Debug, Clone)]
13pub struct ClientError(Box<Inner>);
14
15#[derive(Debug, Clone)]
16struct Inner {
17 status: Status,
18 details: Details,
19}
20
21impl ClientError {
22 pub fn code(&self) -> tonic::Code {
24 self.0.status.code()
25 }
26
27 pub fn message(&self) -> &str {
29 self.0.status.message()
30 }
31
32 pub fn reason(&self) -> Option<&str> {
34 self.0.details.reason.as_deref()
35 }
36
37 pub fn domain(&self) -> Option<&str> {
39 self.0.details.domain.as_deref()
40 }
41
42 pub fn path(&self) -> Option<&str> {
44 self.0.details.path.as_deref()
45 }
46
47 pub fn public_message(&self) -> Option<&str> {
49 self.0.details.public_message.as_deref()
50 }
51
52 pub fn debug(&self) -> Option<&str> {
54 self.0.details.debug.as_deref()
55 }
56
57 pub fn status(&self) -> &Status {
59 &self.0.status
60 }
61
62 pub fn into_status(self) -> Status {
64 self.0.status
65 }
66
67 pub fn into_error(self) -> redup_bootstrap_grpc::Error {
69 redup_bootstrap_grpc::Error::from_status(self.0.status)
70 }
71
72 pub(crate) fn from_status(status: Status) -> Self {
74 Self(Box::new(Inner {
75 details: details(&status),
76 status,
77 }))
78 }
79}
80
81impl From<Status> for ClientError {
82 fn from(status: Status) -> Self {
83 Self::from_status(status)
84 }
85}
86
87impl From<ClientError> for Status {
88 fn from(error: ClientError) -> Self {
89 error.0.status
90 }
91}
92
93impl From<ClientError> for redup_bootstrap_grpc::Error {
94 fn from(error: ClientError) -> Self {
95 error.into_error()
96 }
97}
98
99impl fmt::Display for ClientError {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 write!(f, "{}: {}", self.code_name(), self.0.status.message())
102 }
103}
104
105impl std::error::Error for ClientError {}
106
107impl ClientError {
108 fn code_name(&self) -> &'static str {
110 code_name(self.0.status.code())
111 }
112}
113
114pub fn code_name(code: tonic::Code) -> &'static str {
116 match code {
117 tonic::Code::Ok => "OK",
118 tonic::Code::Cancelled => "Cancelled",
119 tonic::Code::Unknown => "Unknown",
120 tonic::Code::InvalidArgument => "InvalidArgument",
121 tonic::Code::DeadlineExceeded => "DeadlineExceeded",
122 tonic::Code::NotFound => "NotFound",
123 tonic::Code::AlreadyExists => "AlreadyExists",
124 tonic::Code::PermissionDenied => "PermissionDenied",
125 tonic::Code::ResourceExhausted => "ResourceExhausted",
126 tonic::Code::FailedPrecondition => "FailedPrecondition",
127 tonic::Code::Aborted => "Aborted",
128 tonic::Code::OutOfRange => "OutOfRange",
129 tonic::Code::Unimplemented => "Unimplemented",
130 tonic::Code::Internal => "Internal",
131 tonic::Code::Unavailable => "Unavailable",
132 tonic::Code::DataLoss => "DataLoss",
133 tonic::Code::Unauthenticated => "Unauthenticated",
134 }
135}