-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy patherror.rs
148 lines (123 loc) · 3.51 KB
/
error.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
use actix_web::{error::JsonPayloadError, http::StatusCode, HttpResponse, ResponseError};
use colored::Colorize;
use diesel::result::Error as DieselError;
use redis::RedisError;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::backtrace::Backtrace;
use std::collections::HashMap;
use std::result::Result as StdResult;
use std::{
error::Error,
fmt::{Display, Formatter},
};
pub type Result<T> = StdResult<T, InternalError>;
/// Trait for all errors that can be returned by editoast
pub trait EditoastError: Error + Send + Sync {
fn get_status(&self) -> StatusCode;
fn get_type(&self) -> &'static str;
fn context(&self) -> HashMap<String, Value> {
Default::default()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct InternalError {
#[serde(skip)]
status: StatusCode,
#[serde(rename = "type", skip_deserializing)]
error_type: &'static str,
context: HashMap<String, Value>,
message: String,
}
impl InternalError {
pub fn get_type(&self) -> &'static str {
self.error_type
}
pub fn get_status(&self) -> StatusCode {
self.status
}
pub fn get_context(&self) -> &HashMap<String, Value> {
&self.context
}
pub fn with_context<S: AsRef<str>, V: Into<Value>>(mut self, key: S, value: V) -> Self {
self.context.insert(key.as_ref().into(), value.into());
self
}
}
impl Error for InternalError {}
impl Display for InternalError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl<T: EditoastError> From<T> for InternalError {
fn from(err: T) -> Self {
InternalError {
status: err.get_status(),
error_type: err.get_type(),
context: err.context(),
message: err.to_string(),
}
}
}
impl ResponseError for InternalError {
fn status_code(&self) -> StatusCode {
self.status
}
fn error_response(&self) -> HttpResponse {
log::error!(
"[{}] {}: {}",
self.error_type.bold(),
self.message,
Backtrace::capture() // won't log unless RUST_BACKTRACE=1
);
HttpResponse::build(self.status).json(self)
}
}
/// Handle all diesel errors
impl EditoastError for DieselError {
fn get_status(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn get_type(&self) -> &'static str {
"editoast:DieselError"
}
}
/// Handle all redis errors
impl EditoastError for RedisError {
fn get_status(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn get_type(&self) -> &'static str {
"editoast:RedisError"
}
}
/// Handle all json errors
impl EditoastError for JsonPayloadError {
fn get_status(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
fn get_type(&self) -> &'static str {
"editoast:JsonError"
}
fn context(&self) -> HashMap<String, Value> {
[("cause".into(), json!(self.to_string()))].into()
}
}
/// Handle database pool errors
impl EditoastError for diesel_async::pooled_connection::deadpool::PoolError {
fn get_status(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn get_type(&self) -> &'static str {
"editoast:DatabePoolError"
}
}
impl EditoastError for reqwest::Error {
fn get_status(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn get_type(&self) -> &'static str {
"editoast:ReqwestError"
}
}