-
Notifications
You must be signed in to change notification settings - Fork 5
/
errors.rs
434 lines (359 loc) · 14.9 KB
/
errors.rs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// LCOV_EXCL_START
#![allow(missing_docs)]
use std::error::Error as StdError;
use std::fmt::{Display, Formatter};
use std::fmt::Error as FmtError;
use std::result::Result as StdResult;
use diesel::result::Error as DieselError;
use iron::{IronError, Response};
use iron::modifier::Modifier;
use iron::status::Status;
use ruma_identifiers::{RoomId, UserId};
use serde_json;
use i18n::*;
macro_rules! simple_error {
($e:expr) => {
Error{
error_chain: $e.into(),
user_message: None,
}
};
}
macro_rules! user_error {
($e:expr, $u:expr) => {
Error{
error_chain: $e.into(),
user_message: Some($u),
}
}
}
macro_rules! bail_error {
($e:expr) => {
return Err(simple_error!($e));
};
($e:expr, $u:expr) => {
return Err(user_error!($e, $u));
};
}
macro_rules! body_params_error {
($e:expr) => {
ErrorKind::InvalidJSON(format!("Could not serialize {} body params", $e))
};
}
/// `ErrorResponse` defines the format that is used to send an error response as JSON.
#[derive(Serialize)]
struct ErrorResponse {
error: String,
causes: Vec<String>,
}
/// Response from the Matrix homeserver when an error occurred
#[derive(Deserialize, Serialize)]
pub struct MatrixErrorResponse {
/// Error code returned by the Matrix API
pub errcode: String,
/// Error message returned by the Matrix API
pub error: String,
}
/// Response from the Rocket.Chat server when an error occurred
#[derive(Deserialize, Serialize)]
pub struct RocketchatErrorResponse {
/// Status returned by the Rocket.Chat API
pub status: Option<String>,
/// Error message returned by the Rocket.Chat API
pub message: Option<String>,
/// The error that occured
pub error: Option<String>,
}
#[derive(Debug)]
pub struct Error {
/// The chained errors
pub error_chain: ErrorChain,
/// An optional message that is shown to the user
pub user_message: Option<I18n>,
}
pub type Result<T> = StdResult<T, Error>;
error_chain!{
types {
ErrorChain, ErrorKind, ResultExt;
}
errors {
InvalidAccessToken(token: String) {
description("The provided access token is not valid")
display("Could not process request, the access token `{}` is not valid", token)
}
MissingAccessToken {
description("The access token is missing")
display("Could not process request, no access token was provided")
}
InvalidRocketchatToken(token: String) {
description("The provided access token is not valid")
display("Could not process request, the access token `{}` did not match any bridged Rocket.Chat server", token)
}
MissingRocketchatToken {
description("The Rocket.Chat token is missing")
display("Could not process request, no Rocket.Chat token provided")
}
InvalidJSON(msg: String) {
description("The provided data is not valid.")
display("Could not process request, the submitted data is not valid: {}", msg)
}
InvalidYAML(msg: String) {
description("The provided YAML is not valid.")
display("The submitted data is not valid YAML: {}", msg)
}
InvalidUserId(user_id: String) {
description("The provided user ID is not valid")
display("The provided user ID {} is not valid", user_id)
}
InvalidRoomAliasId(room_alias_id: String) {
description("The provided room alias ID is not valid")
display("The provided room alias ID {} is not valid", room_alias_id)
}
InvalidHostname(hostname: String) {
description("The provided hostname ist not valid")
display("The provided hostname {} is not valid", hostname)
}
EventIdGenerationFailed{
description("Generating a new event ID failed")
display("Could not generate a new event ID")
}
UnsupportedHttpMethod(method: String) {
description("The REST API was called with an unsupported method")
display("Unsupported HTTP method {}", method)
}
AuthenticationFailed(error_msg: String) {
description("Authentication failed")
display("Authentication failed: {}", error_msg)
}
ApiCallFailed(url: String) {
description("Call to REST API failed")
display("Could not call REST API endpoint {}", url)
}
MatrixError(error_msg: String) {
description("Errors returned by the Matrix homeserver")
display("Matrix error: {}", error_msg)
}
UnsupportedMatrixApiVersion(versions: String) {
description("The homeserver's API version is not compatible with the application service")
display("No supported API version found for the Matrix homeserver, found versions: {}", versions)
}
RocketchatError(error_msg: String) {
description("Errors returned by the Rocket.Chat API")
display("Rocket.Chat error: {}", error_msg)
}
NoRocketchatServer(url: String){
description("The server is not a Rocket.Chat server")
display("No Rocket.Chat server found when querying {} (version information is missing from the response)", url)
}
RocketchatServerUnreachable(url: String) {
description("The Rocket.Chat is not reachable")
display("Could not reach Rocket.Chat server {}", url)
}
UnsupportedRocketchatApiVersion(min_version: String, versions: String) {
description("The Rocket.Chat server's version is not compatible with the application service")
display("No supported API version (>= {}) found for the Rocket.Chat server, found version: {}",
min_version,
versions
)
}
ReadFileError(path: String) {
description("Error when reading a file")
display("Reading file from {} failed", path)
}
RoomNotConnected(matrix_room_id: String) {
description("The room is not connected, but has to be for the command the user submitted")
display("Room {} is not connected to a Rocket.Chat server, cannot execute command", matrix_room_id)
}
RoomAlreadyConnected(matrix_room_id: String) {
description("The Room is already connected to a Rocket.Chat server")
display("Room {} is already connected", matrix_room_id)
}
AdminRoomForRocketchatServerNotFound(rocketchat_url: String) {
description("The user does not have an admin room that is connected to the given Rocket.Chat server")
display("No admin room found that is connected to the Rocket.Chat server {}", rocketchat_url)
}
RocketchatTokenMissing{
description("A token is needed to connect new Rocket.Chat servers")
display("Attempt to connect a Rocket.Chat server without a token")
}
RocketchatServerAlreadyConnected(rocketchat_url: String) {
description("The Rocket.Chat server is already connected to the application service")
display("Attempt to connect {}, but the Rocket.Chat server is already connected", rocketchat_url)
}
ConnectWithoutRocketchatServerId{
description("Connect command without a Rocket.Chat server ID.")
display("Attempt to connect to a Rocket.Chat server without an ID")
}
ConnectWithInvalidRocketchatServerId(rocketchat_server_id: String) {
description("Connect command with an invalid Rocket.Chat server ID.")
display("Attempt to connect with the Rocket.Chat server ID {}, which contains invalid characters.",
rocketchat_server_id)
}
RocketchatServerIdAlreadyInUse(rocketchat_server_id: String) {
description("Connecting with a Rocket.Chat server id that is already in use")
display("Attempt to connect with the Rocket.Chat server ID {}, which is already in use.", rocketchat_server_id)
}
RocketchatTokenAlreadyInUse(token: String) {
description("The token is already used by another server")
display("The token {} is already in use by another server", token)
}
RocketchatChannelNotFound(channel_name: String) {
description("No channel with the given name found on the Rocket.Chat server")
display("The channel {} does not exist on the Rocket.Chat server", channel_name)
}
RocketchatChannelAlreadyBridged(channel_name: String) {
description("The channel with the given name is already bridged")
display("The channel {} is already bridged", channel_name)
}
RocketchatJoinFirst(channel_name: String) {
description("The user has to join the channel on Rocket.Chat before it can be bridged")
display("Bridging the channel {} failed, because the user hasn't joined it on Rocket.Chat", channel_name)
}
UnbridgeOfNotBridgedRoom(display_name: String) {
description("Room with the given display name could not be found")
display("No room with display_name {} found", display_name)
}
RoomNotEmpty(display_name: String, users: String) {
description("Non virtual users are in the room")
display("The room {} has matrix users ({}) in it, cannot unbridge", display_name, users)
}
ReadConfigError {
description("Error when reading the config content to a string")
display("Could not read config content to string")
}
ServerStartupError {
description("Error when starting the application service")
display("Could not start application service")
}
DatabaseSetupError {
description("Error when setting up the database")
display("Could not setup database")
}
MigrationError {
description("Error when running migrations")
display("Could not run migrations")
}
DBConnectionError {
description("Error when establishing a connection to the database")
display("Could not establish database connection")
}
LoggerExtractionError {
description("Error when getting the logger from the request")
display("Could not get logger from iron")
}
GettingMatrixUserForDirectMessageRoomError {
description("Error when getting matrix user for a direct message room")
display("Could not get matrix user for direct message room")
}
TooManyUsersInAdminRoom(room_id: RoomId) {
description("Too many users in admin room")
display("Room {} has more then two members and cannot be used as admin room", room_id)
}
InviterUnknown(room_id: RoomId) {
description("Inviter for join event was not found")
display("Could not determine if the admin room {} is valid, because the inviter is unknown", room_id)
}
OnlyRoomCreatorCanInviteBotUser(inviter_id: UserId, room_id: RoomId, creator_id: UserId) {
description("Only the room creator can invite the bot user")
display("The bot user was invited by the user {} but the room {} was created by {}, \
bot user is leaving", inviter_id, room_id, creator_id)
}
ConnectionPoolExtractionError {
description("Error when getting the connection pool from the request")
display("Could not get connection pool from iron request")
}
ConnectionPoolCreationError {
description("Error when creating the connection pool")
display("Could not create connection pool")
}
GetConnectionError {
description("Error when getting a connection from the connection pool")
display("Could not get connection from connection pool")
}
DBInsertError {
description("Error when inserting a record")
display("Inserting record into the database failed")
}
DBUpdateError {
description("Error when editing a record")
display("Editing record in the database failed")
}
DBSelectError {
description("Error when selecting a record")
display("Select record from the database failed")
}
DBDeleteError {
description("Error when deleting a record")
display("Deleting record from the database failed")
}
InternalServerError {
description("An internal error")
display("An internal error occurred")
}
}
}
impl Error {
pub fn status_code(&self) -> Status {
match *self.error_chain {
ErrorKind::InvalidAccessToken(_) |
ErrorKind::InvalidRocketchatToken(_) => Status::Forbidden,
ErrorKind::MissingAccessToken |
ErrorKind::MissingRocketchatToken |
ErrorKind::AuthenticationFailed(_) => Status::Unauthorized,
ErrorKind::InvalidJSON(_) => Status::UnprocessableEntity,
ErrorKind::AdminRoomForRocketchatServerNotFound(_) => Status::NotFound,
_ => Status::InternalServerError,
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
self.error_chain.description()
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> StdResult<(), FmtError> {
write!(f, "{}", self.error_chain)
}
}
impl From<ErrorChain> for Error {
fn from(error: ErrorChain) -> Error {
simple_error!(error)
}
}
impl From<ErrorKind> for Error {
fn from(error: ErrorKind) -> Error {
simple_error!(error)
}
}
impl From<DieselError> for Error {
fn from(error: DieselError) -> Error {
simple_error!(format!("{}", error))
}
}
impl From<Error> for IronError {
fn from(error: Error) -> IronError {
let response = Response::with(&error);
IronError {
error: Box::new(error),
response: response,
}
}
}
impl<'a> Modifier<Response> for &'a Error {
fn modify(self, response: &mut Response) {
let error_message = match self.user_message {
Some(ref user_message) => user_message.l(DEFAULT_LANGUAGE),
None => format!("{}", self),
};
let causes = self.error_chain.iter().skip(1).map(|e| format!("{}", e)).collect();
let resp = ErrorResponse {
error: error_message,
causes: causes,
};
let err_msg = serde_json::to_string(&resp).expect("ErrorResponse is always serializable");
response.status = Some(self.status_code());
response.body = Some(Box::new(err_msg));
}
}
// LCOV_EXCL_STOP