forked from OKEAMAH/didkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
47 lines (42 loc) · 1.02 KB
/
error.rs
File metadata and controls
47 lines (42 loc) · 1.02 KB
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
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use tracing::{debug, error};
#[derive(Debug, Clone)]
pub struct Error {
status: StatusCode,
body: ErrorBody,
}
#[derive(Debug, Clone)]
pub enum ErrorBody {
Text(String),
// Json(serde_json::Value),
}
impl From<(StatusCode, String)> for Error {
fn from(e: (StatusCode, String)) -> Error {
Error {
status: e.0,
body: ErrorBody::Text(e.1),
}
}
}
impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
error!("{:?}", e);
Error {
status: StatusCode::INTERNAL_SERVER_ERROR,
body: ErrorBody::Text(e.to_string()),
}
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
match self.body {
ErrorBody::Text(t) => {
debug!("{t}");
(self.status, t).into_response()
} // ErrorBody::Json(j) => (self.status, axum::Json(j)).into_response(),
}
}
}