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
use thiserror::Error;
use arangors::{ClientError};
#[cfg(feature = "actix_http_error")]
use actix_web::{error, http::StatusCode};
#[derive(Error, Debug)]
pub enum ServiceError {
#[error("Internal error")]
InternalError,
#[error("Validations failed: `{0}`")]
ValidationError(String),
#[error("Timeout")]
Timeout,
#[error("{0}")]
NotFound(String),
#[error("Unprocessable entity")]
UnprocessableEntity,
#[error("Unauthorized")]
Unauthorized,
#[error("Forbidden")]
Forbidden,
#[error("Conflict")]
Conflict,
}
#[cfg(feature = "actix_http_error")]
impl error::ResponseError for ServiceError {
fn status_code(&self) -> StatusCode {
match self {
Self::ValidationError(_str) => StatusCode::BAD_REQUEST,
Self::Timeout => StatusCode::REQUEST_TIMEOUT,
Self::NotFound(_str) => StatusCode::NOT_FOUND,
Self::UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY,
Self::Unauthorized => StatusCode::UNAUTHORIZED,
Self::Forbidden => StatusCode::FORBIDDEN,
Self::Conflict=> StatusCode::CONFLICT,
_ => StatusCode::INTERNAL_SERVER_ERROR
}
}
}
impl ServiceError {
pub fn http_code(&self) -> String {
match self {
ServiceError::NotFound(_str) => "404".to_string(),
ServiceError::ValidationError(_str) => "400".to_string(),
ServiceError::UnprocessableEntity => "422".to_string(),
ServiceError::Conflict => "409".to_string(),
ServiceError::Unauthorized => "401".to_string(),
ServiceError::Forbidden => "403".to_string(),
ServiceError::Timeout => "408".to_string(),
_ => "500".to_string()
}
}
}
impl From<ClientError> for ServiceError {
fn from(error: ClientError) -> Self {
match error {
ClientError::Arango(arango_error) => {
match arango_error.code() {
404 => Self::NotFound(arango_error.message().to_string()),
409 => Self::Conflict,
403 => Self::Forbidden,
401 => Self::Unauthorized,
408 => Self::Timeout,
_ => Self::UnprocessableEntity
}
},
ClientError::Serde(_serde_error) => Self::UnprocessableEntity,
ClientError::InsufficientPermission { permission: _permission, operation: _operation } => Self::Unauthorized,
ClientError::InvalidServer(_server) => Self::Unauthorized,
ClientError::HttpClient(_client) => Self::UnprocessableEntity,
}
}
}
impl Default for ServiceError {
fn default() -> Self {
ServiceError::InternalError
}
}