-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresult.rs
139 lines (128 loc) · 4.34 KB
/
result.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
use crate::*;
/// Basic Result alias with [`enum@prest::Error`]
pub type Result<T = (), E = Error> = std::result::Result<T, E>;
/// Utility for type inference that allows using `?` operator in closure handlers
pub const OK: prest::Result<(), Error> = prest::Result::Ok(());
/// Utility for type inference that allows using `?` operator in closure handlers
pub const fn ok<T: IntoResponse>(resp: T) -> Result<T, Error> {
Ok(resp)
}
use thiserror::Error;
/// Error type used across prest codebase
#[derive(Error, Debug)]
pub enum Error {
#[error("Internal")]
Internal,
#[error("Unauthorized")]
Unauthorized,
#[error("Not found")]
NotFound,
#[error(transparent)]
Env(#[from] std::env::VarError),
#[error(transparent)]
ChronoParse(#[from] chrono::ParseError),
#[error(transparent)]
UuidParse(#[from] uuid::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
HttpError(#[from] http::Error),
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
#[cfg(all(host, feature = "auth"))]
#[error(transparent)]
Session(#[from] tower_sessions::session_store::Error),
#[cfg(all(host, feature = "auth"))]
#[error(transparent)]
Auth(#[from] crate::host::auth::authn::AuthError),
#[cfg(all(host, feature = "auth"))]
#[error(transparent)]
Login(#[from] ::axum_login::Error<Prest>),
#[cfg(all(host, feature = "auth"))]
#[error(transparent)]
OpenIDClaimVerification(#[from] openidconnect::ClaimsVerificationError),
#[cfg(feature = "db")]
#[error(transparent)]
SQL(#[from] gluesql_core::error::Error),
#[cfg(all(host, feature = "db"))]
#[error(transparent)]
IO(#[from] std::io::Error),
#[cfg(all(host, feature = "db"))]
#[error(transparent)]
Bincode(#[from] bitcode::Error),
#[error(transparent)]
FormRejection(#[from] axum::extract::rejection::FormRejection),
#[error(transparent)]
QueryRejection(#[from] axum::extract::rejection::QueryRejection),
#[error(transparent)]
JsonRejection(#[from] axum::extract::rejection::JsonRejection),
#[cfg(host)]
#[error(transparent)]
RuSSH(#[from] russh::Error),
#[cfg(host)]
#[error(transparent)]
RuSFTP(#[from] russh_sftp::client::error::Error),
#[cfg(host)]
#[error(transparent)]
Channel(#[from] tokio::sync::oneshot::error::RecvError),
#[error("{0:?}")]
Any(AnyError),
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
match self {
Error::FormRejection(e) => e.into_response(),
Error::QueryRejection(e) => e.into_response(),
Error::JsonRejection(e) => e.into_response(),
Error::Unauthorized => StatusCode::UNAUTHORIZED.into_response(),
#[cfg(all(host, feature = "auth"))]
Error::Login(_)
| Error::Auth(_)
| Error::Session(_)
| Error::OpenIDClaimVerification(_) => StatusCode::UNAUTHORIZED.into_response(),
Error::NotFound => StatusCode::NOT_FOUND.into_response(),
_ => {
error!("{self}");
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
}
}
}
/// Provides shorthand to map errs into [`prest::Error`] using `.somehow()`
#[doc(hidden)]
pub trait _Somehow<T, E> {
fn somehow(self) -> Result<T, Error>;
}
impl<T, E> _Somehow<T, E> for Result<T, E>
where
E: std::fmt::Display,
{
fn somehow(self) -> Result<T, Error> {
self.map_err(|e| Error::Any(AnyError(format!("{e}"))))
}
}
#[derive(Debug)]
#[doc(hidden)]
pub struct AnyError(pub String);
impl<E: std::error::Error> From<E> for AnyError {
fn from(value: E) -> Self {
AnyError(format!("{value}"))
}
}
/// Anyhow-like result which can be `?` from any error type
pub type Somehow<T = ()> = std::result::Result<T, AnyError>;
/// Shorthand to create formatted [`prest::Error`] values like `e!("{x:?}")`
#[macro_export]
macro_rules! e {
($($tokens:tt),+) => {
prest::Error::Any(prest::AnyError(format!($($tokens),+)))
};
}
// #[cfg(host)]
// impl From<::sled::transaction::ConflictableTransactionError<Box<bitcode::ErrorKind>>> for Error {
// fn from(
// value: ::sled::transaction::ConflictableTransactionError<Box<bitcode::ErrorKind>>,
// ) -> Self {
// e!("{value}")
// }
// }