-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.rs
230 lines (163 loc) · 5.57 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
use std::{panic::Location, backtrace::Backtrace, borrow::Cow};
use axum::{http::{StatusCode, HeaderMap, Response, self, HeaderValue, header::ToStrError},response::IntoResponse, body::Body};
use thiserror::Error;
use http_error_derive::HttpError;
use std::convert::From;
// pub trait BackendErrors: std::fmt::Debug + Send {
// fn report_to(&self) -> ErrorReport;
// }
// impl BackendErrors for FragmentError<E> {
// fn report_to(&self) -> ErrorReport {
// self.into_report()
// }
// }
pub trait OptionExt<V, E, T>
{
fn to_result(self, method: T) -> Result<V, E>;
}
impl<V, E, T> OptionExt<V, E, T> for Option<V>
where
E: Into<ErrorStates>,
T: FnOnce() -> E
{
fn to_result(self, method: T) -> Result<V, E>
{
let err = method();
Err(err)
}
}
#[derive(Debug)]
pub struct ErrorReport {
reason: ErrorStates,
resp_code: StatusCode,
headers: HeaderMap
}
impl ErrorReport {
pub fn new(state: ErrorStates, header: HeaderMap) -> Self {
Self {
reason: state,
resp_code: StatusCode::BAD_REQUEST,
headers: header
}
}
pub fn convert_to_response(self) -> Response<Body> {
// let mut json_content = axum::Json::from(self.reason.http_message().unwrap());
let body = Body::from(self.reason.http_message().unwrap());
let mut resp = Response::new(body);
let mut headers = resp.headers_mut();
headers.extend(self.headers);
*resp.status_mut() = self.resp_code;
resp
}
}
impl IntoResponse for FragmentError {
fn into_response(self) -> Response<Body> {
self.into_report().convert_to_response()
}
}
#[derive(Debug)]
pub struct FragmentError {
// location: Location<'a>,
// error: E,
error_state: ErrorStates,
method: String,
trace: Backtrace
}
unsafe impl std::marker::Send for FragmentError { }
impl<'a, E> std::convert::From<E> for FragmentError
where E: Into<ErrorStates>
{
#[track_caller]
fn from(err: E) -> FragmentError {
let loc = Location::caller();
println!("loc: {}", loc);
Self {
// location: Location::caller().to_owned(),
// error: err,
error_state: err.into(),
method: "".to_string(), //TODO function capture for the location
trace: std::backtrace::Backtrace::capture()
}
}
}
impl<'a> FragmentError {
pub fn into_report(self) -> ErrorReport {
let statuscode = StatusCode::from_u16(
self.error_state
.http_code()
.unwrap_or(404)
).unwrap();
let mut headers = vec![
(http::header::CONTENT_TYPE, HeaderValue::from_str("application/json").unwrap())
];
ErrorReport {
reason: self.error_state,
resp_code: statuscode,
headers: HeaderMap::from_iter(headers)
}
}
}
#[derive(Debug, thiserror::Error, HttpError)]
pub enum ErrorStates {
#[http(code = 500, message = "server went into undesired mode")]
#[error("Join Handle Error")]
TaskError(#[from] tokio::task::JoinError),
#[http(code = 500, message = "server went into undesired mode")]
#[error("Tokio IO Error")]
BufferError(#[from] tokio::io::Error),
#[http(code = 500, message = "server went into undesired mode")]
#[error("internal axum Error")]
InternalError(#[from] axum::Error),
#[http(code = 404, message = "Missing Header Field")]
#[error(transparent)]
RequestError(#[from] HeaderErrors<'static>),
#[http(code = 404, message = "Incorrect body type")]
#[error(transparent)]
BodyContentError(#[from] BodyErrors<'static>),
#[http(code = 500, message = "string parsing error")]
#[error("internal parsing error")]
ConvertionErr(#[from] ToStrError),
#[http(code = 404, message = "Invalid input recieved")]
#[error("invalid string present at")]
UuidConvertionErr(#[from] uuid::Error),
#[http(code = 500, message = "Internal server error")]
#[error("axum error")]
AxumHttpError(#[from] axum::http::Error),
#[http(code = 500, message = "Internal server error")]
#[error("Logical processing error")]
UndeclaredError,
// #[http(code = 500, message = "server went into undesired mode")]
// #[error("internal socket Error")]
// SocketError(#[from] ),
// #[http(code = 500, message = "server went into undesired mode")]
// #[error("internal axum Error")]
// UndefinedError(Box<dyn std::error::Error>),
}
#[derive(Debug, thiserror::Error, HttpError)]
pub enum HeaderErrors<'a> {
#[error("Missing header field: {0:?}")]
HeaderFieldMissing(Cow<'a, str>),
#[error("Field mismatch: {0:?}")]
FieldMismatch(Cow<'a, str>),
#[error("Invalid field input: {0:?}")]
InvalidField(Cow<'a, str>),
#[error("Invalid field input: {0:?}")]
HeaderUnwrapError(#[from] http::header::ToStrError),
// #[error("Invalid field input: {0:?}")]
// InvalidField(Cow<'a, str>)
}
#[derive(Debug, thiserror::Error, HttpError)]
pub enum BodyErrors<'a> {
#[error("")]
MissingField(Cow<'a, str>),
#[error("Missing header value in field: {0:?}")]
MissingValueField(Cow<'a, str>),
#[error("Invalid values in field: {0:?}")]
InvalidValues(Cow<'a, str>),
}
// impl<'a, T> std::convert::From<T> for HeaderErrors<'a>
// where T: std::error::Error {
// fn from(val: T) -> Self {
// val.into()
// }
// }