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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use std::fmt;
#[derive(Clone, Debug, PartialEq)]
pub struct Mismatch<T> {
pub expected: T,
pub got: T,
}
impl<T: fmt::Display> fmt::Display for Mismatch<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!("Expected: {}, Got: {}", self.expected, self.got))
}
}
#[derive(Debug, thiserror::Error)]
pub enum CallError {
#[error("Invalid params in the call: {0}")]
InvalidParams(#[source] anyhow::Error),
#[error("RPC Call failed: {0}")]
Failed(#[from] anyhow::Error),
#[error("RPC Call failed: code: {code}, message: {message}, data: {data:?}")]
Custom {
code: i32,
message: String,
data: Option<Box<RawValue>>,
},
}
impl CallError {
pub fn from_std_error<E>(err: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
CallError::Failed(err.into())
}
}
impl From<anyhow::Error> for Error {
fn from(err: anyhow::Error) -> Self {
Error::Call(CallError::Failed(err))
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Server call failed: {0}")]
Call(#[from] CallError),
#[error("Networking or low-level protocol error: {0}")]
Transport(#[source] anyhow::Error),
#[error("JSON-RPC request error: {0:?}")]
Request(String),
#[error("Frontend/backend channel error: {0}")]
Internal(#[from] futures_channel::mpsc::SendError),
#[error("Invalid response: {0}")]
InvalidResponse(Mismatch<String>),
#[error("The background task been terminated because: {0}; restart required")]
RestartNeeded(String),
#[error("Parse error: {0}")]
ParseError(#[from] serde_json::Error),
#[error("Invalid subscription ID")]
InvalidSubscriptionId,
#[error("Invalid request ID")]
InvalidRequestId,
#[error("Unregistered notification method")]
UnregisteredNotification(String),
#[error("A request with the same request ID has already been registered")]
DuplicateRequestId,
#[error("Method: {0} was already registered")]
MethodAlreadyRegistered(String),
#[error("Method: {0} has not yet been registered")]
MethodNotFound(String),
#[error("Cannot use the same method name for subscribe and unsubscribe, used: {0}")]
SubscriptionNameConflict(String),
#[error("Subscription closed: {0:?}")]
SubscriptionClosed(SubscriptionClosedError),
#[error("Request timeout")]
RequestTimeout,
#[error("Configured max number of request slots exceeded")]
MaxSlotsExceeded,
#[error("Attempted to stop server that is already stopped")]
AlreadyStopped,
#[error("Must set at least one allowed value for the {0} header")]
EmptyAllowList(&'static str),
#[error("Resource at capacity: {0}")]
ResourceAtCapacity(&'static str),
#[error("Resource name already taken: {0}")]
ResourceNameAlreadyTaken(&'static str),
#[error("Resource name `{0}` not found for method `{1}`")]
ResourceNameNotFoundForMethod(&'static str, &'static str),
#[error("Method `{0}` has uninitialized resources")]
UninitializedMethod(Box<str>),
#[error("Maximum number of resources reached")]
MaxResourcesReached,
#[error("Custom error: {0}")]
Custom(String),
}
impl Error {
pub fn to_call_error<E>(err: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Error::Call(CallError::from_std_error(err))
}
}
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct SubscriptionClosedError {
subscription_closed: String,
id: u64,
}
impl SubscriptionClosedError {
pub fn new(reason: impl Into<String>, id: u64) -> Self {
Self { subscription_closed: reason.into(), id }
}
pub fn close_reason(&self) -> &str {
&self.subscription_closed
}
pub fn subscription_id(&self) -> u64 {
self.id
}
}
#[derive(Debug, thiserror::Error)]
pub enum GenericTransportError<T: std::error::Error + Send + Sync> {
#[error("The request was too big")]
TooLarge,
#[error("Malformed request")]
Malformed,
#[error("Transport error: {0}")]
Inner(T),
}
impl From<std::io::Error> for Error {
fn from(io_err: std::io::Error) -> Error {
Error::Transport(io_err.into())
}
}
impl From<soketto::handshake::Error> for Error {
fn from(handshake_err: soketto::handshake::Error) -> Error {
Error::Transport(handshake_err.into())
}
}
impl From<soketto::connection::Error> for Error {
fn from(conn_err: soketto::connection::Error) -> Error {
Error::Transport(conn_err.into())
}
}
impl From<hyper::Error> for Error {
fn from(hyper_err: hyper::Error) -> Error {
Error::Transport(hyper_err.into())
}
}