error.rs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Error generator macro
  3. //
  4. use std::error::Error as StdError;
  5. macro_rules! make_error {
  6. ( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)? ) => {
  7. const BAD_REQUEST: u16 = 400;
  8. pub enum ErrorKind { $($name( $ty )),+ }
  9. pub struct Error { message: String, error: ErrorKind, error_code: u16 }
  10. $(impl From<$ty> for Error {
  11. fn from(err: $ty) -> Self { Error::from((stringify!($name), err)) }
  12. })+
  13. $(impl<S: Into<String>> From<(S, $ty)> for Error {
  14. fn from(val: (S, $ty)) -> Self {
  15. Error { message: val.0.into(), error: ErrorKind::$name(val.1), error_code: BAD_REQUEST }
  16. }
  17. })+
  18. impl StdError for Error {
  19. fn source(&self) -> Option<&(dyn StdError + 'static)> {
  20. match &self.error {$( ErrorKind::$name(e) => $src_fn(e), )+}
  21. }
  22. }
  23. impl std::fmt::Display for Error {
  24. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  25. match &self.error {$(
  26. ErrorKind::$name(e) => f.write_str(&$usr_msg_fun(e, &self.message)),
  27. )+}
  28. }
  29. }
  30. };
  31. }
  32. use diesel::r2d2::PoolError as R2d2Err;
  33. use diesel::result::Error as DieselErr;
  34. use diesel::ConnectionError as DieselConErr;
  35. use diesel_migrations::RunMigrationsError as DieselMigErr;
  36. use handlebars::RenderError as HbErr;
  37. use jsonwebtoken::errors::Error as JwtErr;
  38. use lettre::address::AddressError as AddrErr;
  39. use lettre::error::Error as LettreErr;
  40. use lettre::transport::smtp::Error as SmtpErr;
  41. use openssl::error::ErrorStack as SSLErr;
  42. use regex::Error as RegexErr;
  43. use reqwest::Error as ReqErr;
  44. use rocket::error::Error as RocketErr;
  45. use serde_json::{Error as SerdeErr, Value};
  46. use std::io::Error as IoErr;
  47. use std::time::SystemTimeError as TimeErr;
  48. use u2f::u2ferror::U2fError as U2fErr;
  49. use webauthn_rs::error::WebauthnError as WebauthnErr;
  50. use yubico::yubicoerror::YubicoError as YubiErr;
  51. #[derive(Serialize)]
  52. pub struct Empty {}
  53. // Error struct
  54. // Contains a String error message, meant for the user and an enum variant, with an error of different types.
  55. //
  56. // After the variant itself, there are two expressions. The first one indicates whether the error contains a source error (that we pretty print).
  57. // The second one contains the function used to obtain the response sent to the client
  58. make_error! {
  59. // Just an empty error
  60. Empty(Empty): _no_source, _serialize,
  61. // Used to represent err! calls
  62. Simple(String): _no_source, _api_error,
  63. // Used for special return values, like 2FA errors
  64. Json(Value): _no_source, _serialize,
  65. Db(DieselErr): _has_source, _api_error,
  66. R2d2(R2d2Err): _has_source, _api_error,
  67. U2f(U2fErr): _has_source, _api_error,
  68. Serde(SerdeErr): _has_source, _api_error,
  69. JWt(JwtErr): _has_source, _api_error,
  70. Handlebars(HbErr): _has_source, _api_error,
  71. Io(IoErr): _has_source, _api_error,
  72. Time(TimeErr): _has_source, _api_error,
  73. Req(ReqErr): _has_source, _api_error,
  74. Regex(RegexErr): _has_source, _api_error,
  75. Yubico(YubiErr): _has_source, _api_error,
  76. Lettre(LettreErr): _has_source, _api_error,
  77. Address(AddrErr): _has_source, _api_error,
  78. Smtp(SmtpErr): _has_source, _api_error,
  79. OpenSSL(SSLErr): _has_source, _api_error,
  80. Rocket(RocketErr): _has_source, _api_error,
  81. DieselCon(DieselConErr): _has_source, _api_error,
  82. DieselMig(DieselMigErr): _has_source, _api_error,
  83. Webauthn(WebauthnErr): _has_source, _api_error,
  84. }
  85. impl std::fmt::Debug for Error {
  86. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  87. match self.source() {
  88. Some(e) => write!(f, "{}.\n[CAUSE] {:#?}", self.message, e),
  89. None => match self.error {
  90. ErrorKind::Empty(_) => Ok(()),
  91. ErrorKind::Simple(ref s) => {
  92. if &self.message == s {
  93. write!(f, "{}", self.message)
  94. } else {
  95. write!(f, "{}. {}", self.message, s)
  96. }
  97. }
  98. ErrorKind::Json(_) => write!(f, "{}", self.message),
  99. _ => unreachable!(),
  100. },
  101. }
  102. }
  103. }
  104. impl Error {
  105. pub fn new<M: Into<String>, N: Into<String>>(usr_msg: M, log_msg: N) -> Self {
  106. (usr_msg, log_msg.into()).into()
  107. }
  108. pub fn empty() -> Self {
  109. Empty {}.into()
  110. }
  111. #[must_use]
  112. pub fn with_msg<M: Into<String>>(mut self, msg: M) -> Self {
  113. self.message = msg.into();
  114. self
  115. }
  116. #[must_use]
  117. pub const fn with_code(mut self, code: u16) -> Self {
  118. self.error_code = code;
  119. self
  120. }
  121. }
  122. pub trait MapResult<S> {
  123. fn map_res(self, msg: &str) -> Result<S, Error>;
  124. }
  125. impl<S, E: Into<Error>> MapResult<S> for Result<S, E> {
  126. fn map_res(self, msg: &str) -> Result<S, Error> {
  127. self.map_err(|e| e.into().with_msg(msg))
  128. }
  129. }
  130. impl<E: Into<Error>> MapResult<()> for Result<usize, E> {
  131. fn map_res(self, msg: &str) -> Result<(), Error> {
  132. self.and(Ok(())).map_res(msg)
  133. }
  134. }
  135. impl<S> MapResult<S> for Option<S> {
  136. fn map_res(self, msg: &str) -> Result<S, Error> {
  137. self.ok_or_else(|| Error::new(msg, ""))
  138. }
  139. }
  140. #[allow(clippy::unnecessary_wraps)]
  141. const fn _has_source<T>(e: T) -> Option<T> {
  142. Some(e)
  143. }
  144. fn _no_source<T, S>(_: T) -> Option<S> {
  145. None
  146. }
  147. fn _serialize(e: &impl serde::Serialize, _msg: &str) -> String {
  148. serde_json::to_string(e).unwrap()
  149. }
  150. fn _api_error(_: &impl std::any::Any, msg: &str) -> String {
  151. let json = json!({
  152. "Message": msg,
  153. "error": "",
  154. "error_description": "",
  155. "ValidationErrors": {"": [ msg ]},
  156. "ErrorModel": {
  157. "Message": msg,
  158. "Object": "error"
  159. },
  160. "ExceptionMessage": null,
  161. "ExceptionStackTrace": null,
  162. "InnerExceptionMessage": null,
  163. "Object": "error"
  164. });
  165. _serialize(&json, "")
  166. }
  167. //
  168. // Rocket responder impl
  169. //
  170. use std::io::Cursor;
  171. use rocket::http::{ContentType, Status};
  172. use rocket::request::Request;
  173. use rocket::response::{self, Responder, Response};
  174. impl<'r> Responder<'r, 'static> for Error {
  175. fn respond_to(self, _: &Request) -> response::Result<'static> {
  176. match self.error {
  177. ErrorKind::Empty(_) => {} // Don't print the error in this situation
  178. ErrorKind::Simple(_) => {} // Don't print the error in this situation
  179. _ => error!(target: "error", "{:#?}", self),
  180. };
  181. let code = Status::from_code(self.error_code).unwrap_or(Status::BadRequest);
  182. let body = self.to_string();
  183. Response::build().status(code).header(ContentType::JSON).sized_body(Some(body.len()), Cursor::new(body)).ok()
  184. }
  185. }
  186. //
  187. // Error return macros
  188. //
  189. #[macro_export]
  190. macro_rules! err {
  191. ($msg:expr) => {{
  192. error!("{}", $msg);
  193. return Err(crate::error::Error::new($msg, $msg));
  194. }};
  195. ($usr_msg:expr, $log_value:expr) => {{
  196. error!("{}. {}", $usr_msg, $log_value);
  197. return Err(crate::error::Error::new($usr_msg, $log_value));
  198. }};
  199. }
  200. macro_rules! err_silent {
  201. ($msg:expr) => {{
  202. return Err(crate::error::Error::new($msg, $msg));
  203. }};
  204. ($usr_msg:expr, $log_value:expr) => {{
  205. return Err(crate::error::Error::new($usr_msg, $log_value));
  206. }};
  207. }
  208. #[macro_export]
  209. macro_rules! err_code {
  210. ($msg:expr, $err_code: expr) => {{
  211. error!("{}", $msg);
  212. return Err(crate::error::Error::new($msg, $msg).with_code($err_code));
  213. }};
  214. ($usr_msg:expr, $log_value:expr, $err_code: expr) => {{
  215. error!("{}. {}", $usr_msg, $log_value);
  216. return Err(crate::error::Error::new($usr_msg, $log_value).with_code($err_code));
  217. }};
  218. }
  219. #[macro_export]
  220. macro_rules! err_discard {
  221. ($msg:expr, $data:expr) => {{
  222. std::io::copy(&mut $data.open(), &mut std::io::sink()).ok();
  223. return Err(crate::error::Error::new($msg, $msg));
  224. }};
  225. ($usr_msg:expr, $log_value:expr, $data:expr) => {{
  226. std::io::copy(&mut $data.open(), &mut std::io::sink()).ok();
  227. return Err(crate::error::Error::new($usr_msg, $log_value));
  228. }};
  229. }
  230. #[macro_export]
  231. macro_rules! err_json {
  232. ($expr:expr, $log_value:expr) => {{
  233. return Err(($log_value, $expr).into());
  234. }};
  235. }
  236. #[macro_export]
  237. macro_rules! err_handler {
  238. ($expr:expr) => {{
  239. error!(target: "auth", "Unauthorized Error: {}", $expr);
  240. return ::rocket::request::Outcome::Failure((rocket::http::Status::Unauthorized, $expr));
  241. }};
  242. ($usr_msg:expr, $log_value:expr) => {{
  243. error!(target: "auth", "Unauthorized Error: {}. {}", $usr_msg, $log_value);
  244. return ::rocket::request::Outcome::Failure((rocket::http::Status::Unauthorized, $usr_msg));
  245. }};
  246. }