1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#[path = "google.rpc.rs"]
pub mod rpc;
#[path = ""]
pub mod iam {
#[path = "google.iam.v1.rs"]
pub mod v1;
}
#[path = "google.longrunning.rs"]
pub mod longrunning;
#[path = "google.r#type.rs"]
pub mod r#type;
#[path = ""]
pub mod spanner {
#[path = "google.spanner.v1.rs"]
pub mod v1;
#[path = ""]
pub mod admin {
#[path = ""]
pub mod database {
#[path = "google.spanner.admin.database.v1.rs"]
pub mod v1;
}
#[path = ""]
pub mod instance {
#[path = "google.spanner.admin.instance.v1.rs"]
pub mod v1;
}
}
}
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
pub struct Status {
source: tonic::Status,
}
impl Status {
pub fn new(cause: tonic::Status) -> Self {
Status { source: cause }
}
pub fn code(&self) -> Code {
self.source.code().into()
}
pub fn message(&self) -> &str {
&self.source.message()
}
pub fn details(&self) -> &[u8] {
&self.source.details()
}
}
impl Error for Status {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source.source()
}
}
impl From<tonic::Status> for Status {
fn from(tonic_status: tonic::Status) -> Self {
return Status {
source: tonic_status,
};
}
}
impl Debug for Status {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.source, f)
}
}
impl Display for Status {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.source, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Code {
Ok = 0,
Cancelled = 1,
Unknown = 2,
InvalidArgument = 3,
DeadlineExceeded = 4,
NotFound = 5,
AlreadyExists = 6,
PermissionDenied = 7,
ResourceExhausted = 8,
FailedPrecondition = 9,
Aborted = 10,
OutOfRange = 11,
Unimplemented = 12,
Internal = 13,
Unavailable = 14,
DataLoss = 15,
Unauthenticated = 16,
}
impl From<tonic::Code> for Code {
fn from(tonic_code: tonic::Code) -> Self {
match tonic_code {
tonic::Code::Ok => Code::Ok,
tonic::Code::Cancelled => Code::Cancelled,
tonic::Code::Unknown => Code::Unknown,
tonic::Code::InvalidArgument => Code::InvalidArgument,
tonic::Code::DeadlineExceeded => Code::DeadlineExceeded,
tonic::Code::NotFound => Code::NotFound,
tonic::Code::AlreadyExists => Code::AlreadyExists,
tonic::Code::PermissionDenied => Code::PermissionDenied,
tonic::Code::ResourceExhausted => Code::ResourceExhausted,
tonic::Code::FailedPrecondition => Code::FailedPrecondition,
tonic::Code::Aborted => Code::Aborted,
tonic::Code::OutOfRange => Code::OutOfRange,
tonic::Code::Unimplemented => Code::Unimplemented,
tonic::Code::Internal => Code::Internal,
tonic::Code::Unavailable => Code::Unavailable,
tonic::Code::DataLoss => Code::DataLoss,
tonic::Code::Unauthenticated => Code::Unauthenticated,
}
}
}
impl Code {
pub fn description(&self) -> &'static str {
match self {
Code::Ok => "The operation completed successfully",
Code::Cancelled => "The operation was cancelled",
Code::Unknown => "Unknown error",
Code::InvalidArgument => "Client specified an invalid argument",
Code::DeadlineExceeded => "Deadline expired before operation could complete",
Code::NotFound => "Some requested entity was not found",
Code::AlreadyExists => "Some entity that we attempted to create already exists",
Code::PermissionDenied => {
"The caller does not have permission to execute the specified operation"
}
Code::ResourceExhausted => "Some resource has been exhausted",
Code::FailedPrecondition => {
"The system is not in a state required for the operation's execution"
}
Code::Aborted => "The operation was aborted",
Code::OutOfRange => "Operation was attempted past the valid range",
Code::Unimplemented => "Operation is not implemented or not supported",
Code::Internal => "Internal error",
Code::Unavailable => "The service is currently unavailable",
Code::DataLoss => "Unrecoverable data loss or corruption",
Code::Unauthenticated => "The request does not have valid authentication credentials",
}
}
}
impl Display for Code {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.description(), f)
}
}