error.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #[derive(Display)]
  8. enum ErrorKind { $($name( $ty )),+ }
  9. pub struct Error { message: String, error: ErrorKind }
  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) }
  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::result::Error as DieselError;
  33. use jsonwebtoken::errors::Error as JwtError;
  34. use serde_json::{Error as SerError, Value};
  35. use std::io::Error as IOError;
  36. use u2f::u2ferror::U2fError as U2fErr;
  37. // Error struct
  38. // Contains a String error message, meant for the user and an enum variant, with an error of different types.
  39. //
  40. // After the variant itself, there are two expressions. The first one indicates whether the error contains a source error (that we pretty print).
  41. // The second one contains the function used to obtain the response sent to the client
  42. make_error! {
  43. // Used to represent err! calls
  44. SimpleError(String): _no_source, _api_error,
  45. // Used for special return values, like 2FA errors
  46. JsonError(Value): _no_source, _serialize,
  47. DbError(DieselError): _has_source, _api_error,
  48. U2fError(U2fErr): _has_source, _api_error,
  49. SerdeError(SerError): _has_source, _api_error,
  50. JWTError(JwtError): _has_source, _api_error,
  51. IoErrror(IOError): _has_source, _api_error,
  52. //WsError(ws::Error): _has_source, _api_error,
  53. }
  54. impl std::fmt::Debug for Error {
  55. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  56. match self.source() {
  57. Some(e) => write!(f, "{}.\n[CAUSE] {:#?}", self.message, e),
  58. None => write!(f, "{}. {}", self.message, self.error),
  59. }
  60. }
  61. }
  62. impl Error {
  63. pub fn new<M: Into<String>, N: Into<String>>(usr_msg: M, log_msg: N) -> Self {
  64. (usr_msg, log_msg.into()).into()
  65. }
  66. pub fn with_msg<M: Into<String>>(mut self, msg: M) -> Self {
  67. self.message = msg.into();
  68. self
  69. }
  70. }
  71. pub trait MapResult<S> {
  72. fn map_res(self, msg: &str) -> Result<S, Error>;
  73. }
  74. impl<S, E: Into<Error>> MapResult<S> for Result<S, E> {
  75. fn map_res(self, msg: &str) -> Result<S, Error> {
  76. self.map_err(|e| e.into().with_msg(msg))
  77. }
  78. }
  79. impl<E: Into<Error>> MapResult<()> for Result<usize, E> {
  80. fn map_res(self, msg: &str) -> Result<(), Error> {
  81. self.and(Ok(())).map_res(msg)
  82. }
  83. }
  84. fn _has_source<T>(e: T) -> Option<T> {
  85. Some(e)
  86. }
  87. fn _no_source<T, S>(_: T) -> Option<S> {
  88. None
  89. }
  90. fn _serialize(e: &impl serde::Serialize, _msg: &str) -> String {
  91. serde_json::to_string(e).unwrap()
  92. }
  93. fn _api_error(_: &impl std::any::Any, msg: &str) -> String {
  94. let json = json!({
  95. "Message": "",
  96. "error": "",
  97. "error_description": "",
  98. "ValidationErrors": {"": [ msg ]},
  99. "ErrorModel": {
  100. "Message": msg,
  101. "Object": "error"
  102. },
  103. "Object": "error"
  104. });
  105. _serialize(&json, "")
  106. }
  107. //
  108. // Rocket responder impl
  109. //
  110. use std::io::Cursor;
  111. use rocket::http::{ContentType, Status};
  112. use rocket::request::Request;
  113. use rocket::response::{self, Responder, Response};
  114. impl<'r> Responder<'r> for Error {
  115. fn respond_to(self, _: &Request) -> response::Result<'r> {
  116. let usr_msg = format!("{}", self);
  117. error!("{:#?}", self);
  118. Response::build()
  119. .status(Status::BadRequest)
  120. .header(ContentType::JSON)
  121. .sized_body(Cursor::new(usr_msg))
  122. .ok()
  123. }
  124. }
  125. //
  126. // Error return macros
  127. //
  128. #[macro_export]
  129. macro_rules! err {
  130. ($msg:expr) => {{
  131. return Err(crate::error::Error::new($msg, $msg));
  132. }};
  133. ($usr_msg:expr, $log_value:expr) => {{
  134. return Err(crate::error::Error::new($usr_msg, $log_value));
  135. }};
  136. }
  137. #[macro_export]
  138. macro_rules! err_json {
  139. ($expr:expr) => {{
  140. return Err(crate::error::Error::from($expr));
  141. }};
  142. }
  143. #[macro_export]
  144. macro_rules! err_handler {
  145. ($expr:expr) => {{
  146. error!("Unauthorized Error: {:#?}", $expr);
  147. return rocket::Outcome::Failure((rocket::http::Status::Unauthorized, $expr));
  148. }};
  149. }