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
#[derive(Debug,Clone,PartialEq)]
pub struct ApiError {
pub code: u16,
pub internal_message: String,
pub external_message: String,
pub value: Option<serde_json::Value>
}
impl ApiError {
#[doc(hidden)]
pub const SERVER_ERROR: &'static str = "Internal server error";
pub fn server_error<S: Into<String>>(msg: S) -> ApiError {
ApiError {
code: 500,
internal_message: msg.into(),
external_message: ApiError::SERVER_ERROR.to_owned(),
value: None
}
}
pub fn path_not_found() -> ApiError {
ApiError {
code: 404,
internal_message: "Not found".to_owned(),
external_message: "Not found".to_owned(),
value: None
}
}
pub fn not_authorized(reason: &str) -> ApiError {
let msg = format!("Not Authorized: {}", reason);
ApiError {
code: 403,
external_message: msg.clone(),
internal_message: msg,
value: None
}
}
}
impl From<std::convert::Infallible> for ApiError {
fn from(_: std::convert::Infallible) -> ApiError { unreachable!() }
}