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
208
209
210
211
212
use std::{error::Error as StdError, fmt::Display, str::FromStr};
use bytes::Bytes;
use crate::{response::BoxResponse, BoxError, Response};
crate::include_proto!("hrpc.v1");
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum HrpcErrorIdentifier {
NotImplemented,
ResourceExhausted,
InternalServerError,
Unavailable,
NotFound,
}
impl From<HrpcErrorIdentifier> for String {
fn from(id: HrpcErrorIdentifier) -> Self {
id.as_id().to_string()
}
}
impl AsRef<str> for HrpcErrorIdentifier {
fn as_ref(&self) -> &str {
self.as_id()
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct NotHrpcErrorIdentifier;
impl FromStr for HrpcErrorIdentifier {
type Err = NotHrpcErrorIdentifier;
fn from_str(s: &str) -> Result<Self, Self::Err> {
compare_if_ret::compare_if_ret! {
s,
Self::NotImplemented,
Self::ResourceExhausted,
Self::InternalServerError,
Self::Unavailable,
Self::NotFound,
}
}
}
mod compare_if_ret {
macro_rules! compare_if_ret {
($var:ident, $variant:expr, $( $variant2:expr, )+) => {
if $variant.compare($var) {
return Ok($variant);
} $(
else if $variant2.compare($var) {
return Ok($variant2);
}
)* else {
return Err(NotHrpcErrorIdentifier);
}
};
}
pub(crate) use compare_if_ret;
}
impl HrpcErrorIdentifier {
pub const fn as_id(&self) -> &'static str {
match self {
Self::InternalServerError => "hrpc.internal-server-error",
Self::ResourceExhausted => "hrpc.resource-exhausted",
Self::NotImplemented => "hrpc.not-implemented",
Self::Unavailable => "hrpc.unavailable",
Self::NotFound => "hrpc.not-found",
}
}
pub fn compare(&self, identifier: impl AsRef<str>) -> bool {
identifier.as_ref() == self.as_id()
}
}
impl Error {
pub fn new_not_implemented(message: impl Into<String>) -> Self {
Self::default()
.with_identifier(HrpcErrorIdentifier::NotImplemented)
.with_message(message)
}
pub fn new_resource_exhausted(message: impl Into<String>) -> Self {
Self::default()
.with_identifier(HrpcErrorIdentifier::ResourceExhausted)
.with_message(message)
}
pub fn new_internal_server_error(message: impl Into<String>) -> Self {
Self::default()
.with_identifier(HrpcErrorIdentifier::InternalServerError)
.with_message(message)
}
pub fn new_not_found(message: impl Into<String>) -> Self {
Self::default()
.with_identifier(HrpcErrorIdentifier::NotFound)
.with_message(message)
}
pub fn with_details(mut self, details: impl Into<Bytes>) -> Self {
self.details = details.into();
self
}
pub fn with_identifier(mut self, identifier: impl Into<String>) -> Self {
self.identifier = identifier.into();
self
}
pub fn with_message(mut self, message: impl Into<String>) -> Self {
self.human_message = message.into();
self
}
pub(crate) fn invalid_hrpc_error(details: impl Into<Bytes>) -> Self {
Self {
human_message:
"the server error was an invalid hRPC error, check more_details field for the error"
.to_string(),
identifier: "hrpcrs.invalid-hrpc-error".to_string(),
details: details.into(),
}
}
}
impl<'a> From<&'a str> for Error {
fn from(msg: &'a str) -> Self {
Error::default()
.with_identifier(HrpcErrorIdentifier::InternalServerError)
.with_message(msg)
}
}
impl<'a, 'b> From<(&'a str, &'b str)> for Error {
fn from((id, msg): (&'a str, &'b str)) -> Self {
Error::default().with_identifier(id).with_message(msg)
}
}
impl From<String> for Error {
fn from(msg: String) -> Self {
Error::default()
.with_identifier(HrpcErrorIdentifier::InternalServerError)
.with_message(msg)
}
}
impl<'a> From<(&'a str, String)> for Error {
fn from((id, msg): (&'a str, String)) -> Self {
Error::default().with_identifier(id).with_message(msg)
}
}
impl From<BoxError> for Error {
fn from(err: BoxError) -> Self {
Error::default().with_message(err.to_string())
}
}
impl From<(&'static str, BoxError)> for Error {
fn from((id, err): (&'static str, BoxError)) -> Self {
Error::default()
.with_identifier(id)
.with_message(err.to_string())
}
}
impl From<Error> for Response<Error> {
fn from(err: Error) -> Self {
let mut resp = Response::new(&err);
resp.extensions_mut().insert(err);
resp
}
}
impl From<Error> for BoxResponse {
fn from(err: Error) -> Self {
Response::<Error>::from(err).map::<()>()
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "error '{}': {}", self.identifier, self.human_message)
}
}
impl StdError for Error {}