2018-12-19 20:52:53 +00:00
|
|
|
//
|
|
|
|
// Error generator macro
|
|
|
|
//
|
2018-12-29 00:01:58 +00:00
|
|
|
use std::error::Error as StdError;
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
macro_rules! make_error {
|
2019-02-02 15:47:27 +00:00
|
|
|
( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)? ) => {
|
2019-03-13 23:17:36 +00:00
|
|
|
const BAD_REQUEST: u16 = 400;
|
|
|
|
|
2019-02-08 17:45:07 +00:00
|
|
|
pub enum ErrorKind { $($name( $ty )),+ }
|
2019-03-13 23:17:36 +00:00
|
|
|
pub struct Error { message: String, error: ErrorKind, error_code: u16 }
|
2018-12-30 22:34:31 +00:00
|
|
|
|
2018-12-29 00:01:58 +00:00
|
|
|
$(impl From<$ty> for Error {
|
|
|
|
fn from(err: $ty) -> Self { Error::from((stringify!($name), err)) }
|
2018-12-19 20:52:53 +00:00
|
|
|
})+
|
2018-12-29 00:01:58 +00:00
|
|
|
$(impl<S: Into<String>> From<(S, $ty)> for Error {
|
|
|
|
fn from(val: (S, $ty)) -> Self {
|
2019-03-13 23:17:36 +00:00
|
|
|
Error { message: val.0.into(), error: ErrorKind::$name(val.1), error_code: BAD_REQUEST }
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
|
|
|
})+
|
2018-12-29 00:01:58 +00:00
|
|
|
impl StdError for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
|
|
|
match &self.error {$( ErrorKind::$name(e) => $src_fn(e), )+}
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
2018-12-29 00:01:58 +00:00
|
|
|
}
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match &self.error {$(
|
|
|
|
ErrorKind::$name(e) => f.write_str(&$usr_msg_fun(e, &self.message)),
|
2018-12-19 20:52:53 +00:00
|
|
|
)+}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:36:33 +00:00
|
|
|
use diesel::result::Error as DieselErr;
|
2020-08-18 15:15:44 +00:00
|
|
|
use diesel::r2d2::PoolError as R2d2Err;
|
2019-01-20 14:36:33 +00:00
|
|
|
use handlebars::RenderError as HbErr;
|
|
|
|
use jsonwebtoken::errors::Error as JWTErr;
|
2019-02-01 22:39:14 +00:00
|
|
|
use regex::Error as RegexErr;
|
2019-01-20 14:36:33 +00:00
|
|
|
use reqwest::Error as ReqErr;
|
|
|
|
use serde_json::{Error as SerdeErr, Value};
|
|
|
|
use std::io::Error as IOErr;
|
2019-04-11 16:40:03 +00:00
|
|
|
|
2019-01-20 14:36:33 +00:00
|
|
|
use std::time::SystemTimeError as TimeErr;
|
2018-12-29 00:01:58 +00:00
|
|
|
use u2f::u2ferror::U2fError as U2fErr;
|
2019-02-01 22:39:14 +00:00
|
|
|
use yubico::yubicoerror::YubicoError as YubiErr;
|
2020-05-06 22:50:30 +00:00
|
|
|
|
2020-05-03 15:41:53 +00:00
|
|
|
use lettre::address::AddressError as AddrErr;
|
2020-05-06 22:50:30 +00:00
|
|
|
use lettre::error::Error as LettreErr;
|
|
|
|
use lettre::message::mime::FromStrError as FromStrErr;
|
2020-09-11 21:52:20 +00:00
|
|
|
use lettre::transport::smtp::Error as SmtpErr;
|
2018-12-19 20:52:53 +00:00
|
|
|
|
2020-05-03 15:24:51 +00:00
|
|
|
#[derive(Serialize)]
|
2019-03-13 23:17:36 +00:00
|
|
|
pub struct Empty {}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
// Error struct
|
2018-12-29 00:01:58 +00:00
|
|
|
// Contains a String error message, meant for the user and an enum variant, with an error of different types.
|
2018-12-21 21:08:04 +00:00
|
|
|
//
|
2018-12-29 00:01:58 +00:00
|
|
|
// After the variant itself, there are two expressions. The first one indicates whether the error contains a source error (that we pretty print).
|
2018-12-19 20:52:53 +00:00
|
|
|
// The second one contains the function used to obtain the response sent to the client
|
|
|
|
make_error! {
|
2019-03-13 23:17:36 +00:00
|
|
|
// Just an empty error
|
|
|
|
EmptyError(Empty): _no_source, _serialize,
|
2018-12-19 20:52:53 +00:00
|
|
|
// Used to represent err! calls
|
2018-12-29 00:01:58 +00:00
|
|
|
SimpleError(String): _no_source, _api_error,
|
2018-12-19 20:52:53 +00:00
|
|
|
// Used for special return values, like 2FA errors
|
2018-12-29 00:01:58 +00:00
|
|
|
JsonError(Value): _no_source, _serialize,
|
2019-01-20 14:36:33 +00:00
|
|
|
DbError(DieselErr): _has_source, _api_error,
|
2020-08-18 15:15:44 +00:00
|
|
|
R2d2Error(R2d2Err): _has_source, _api_error,
|
2018-12-29 00:01:58 +00:00
|
|
|
U2fError(U2fErr): _has_source, _api_error,
|
2019-01-20 14:36:33 +00:00
|
|
|
SerdeError(SerdeErr): _has_source, _api_error,
|
|
|
|
JWTError(JWTErr): _has_source, _api_error,
|
|
|
|
TemplError(HbErr): _has_source, _api_error,
|
2018-12-29 00:01:58 +00:00
|
|
|
//WsError(ws::Error): _has_source, _api_error,
|
2019-01-20 14:36:33 +00:00
|
|
|
IOError(IOErr): _has_source, _api_error,
|
|
|
|
TimeError(TimeErr): _has_source, _api_error,
|
|
|
|
ReqError(ReqErr): _has_source, _api_error,
|
2019-01-27 14:39:19 +00:00
|
|
|
RegexError(RegexErr): _has_source, _api_error,
|
2019-02-01 22:39:14 +00:00
|
|
|
YubiError(YubiErr): _has_source, _api_error,
|
2020-05-06 22:50:30 +00:00
|
|
|
|
2020-09-14 18:47:46 +00:00
|
|
|
LettreError(LettreErr): _has_source, _api_error,
|
2020-05-06 22:50:30 +00:00
|
|
|
AddressError(AddrErr): _has_source, _api_error,
|
|
|
|
SmtpError(SmtpErr): _has_source, _api_error,
|
|
|
|
FromStrError(FromStrErr): _has_source, _api_error,
|
2018-12-29 00:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self.source() {
|
|
|
|
Some(e) => write!(f, "{}.\n[CAUSE] {:#?}", self.message, e),
|
2019-12-07 13:38:32 +00:00
|
|
|
None => match self.error {
|
|
|
|
ErrorKind::EmptyError(_) => Ok(()),
|
|
|
|
ErrorKind::SimpleError(ref s) => {
|
|
|
|
if &self.message == s {
|
|
|
|
write!(f, "{}", self.message)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}. {}", self.message, s)
|
|
|
|
}
|
2019-12-27 17:37:14 +00:00
|
|
|
}
|
2019-12-07 13:38:32 +00:00
|
|
|
ErrorKind::JsonError(_) => write!(f, "{}", self.message),
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2018-12-29 00:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
pub fn new<M: Into<String>, N: Into<String>>(usr_msg: M, log_msg: N) -> Self {
|
2018-12-29 00:01:58 +00:00
|
|
|
(usr_msg, log_msg.into()).into()
|
|
|
|
}
|
|
|
|
|
2019-03-13 23:17:36 +00:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
Empty {}.into()
|
|
|
|
}
|
|
|
|
|
2018-12-29 00:01:58 +00:00
|
|
|
pub fn with_msg<M: Into<String>>(mut self, msg: M) -> Self {
|
|
|
|
self.message = msg.into();
|
|
|
|
self
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
2019-03-13 23:17:36 +00:00
|
|
|
|
2020-05-03 15:24:51 +00:00
|
|
|
pub const fn with_code(mut self, code: u16) -> Self {
|
2019-03-13 23:17:36 +00:00
|
|
|
self.error_code = code;
|
|
|
|
self
|
|
|
|
}
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 23:25:38 +00:00
|
|
|
pub trait MapResult<S> {
|
|
|
|
fn map_res(self, msg: &str) -> Result<S, Error>;
|
2018-12-21 21:08:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 23:25:38 +00:00
|
|
|
impl<S, E: Into<Error>> MapResult<S> for Result<S, E> {
|
2018-12-21 21:08:04 +00:00
|
|
|
fn map_res(self, msg: &str) -> Result<S, Error> {
|
2018-12-29 00:01:58 +00:00
|
|
|
self.map_err(|e| e.into().with_msg(msg))
|
2018-12-21 21:08:04 +00:00
|
|
|
}
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 23:25:38 +00:00
|
|
|
impl<E: Into<Error>> MapResult<()> for Result<usize, E> {
|
2018-12-19 20:52:53 +00:00
|
|
|
fn map_res(self, msg: &str) -> Result<(), Error> {
|
2018-12-21 21:08:04 +00:00
|
|
|
self.and(Ok(())).map_res(msg)
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:40:03 +00:00
|
|
|
impl<S> MapResult<S> for Option<S> {
|
|
|
|
fn map_res(self, msg: &str) -> Result<S, Error> {
|
|
|
|
self.ok_or_else(|| Error::new(msg, ""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:24:51 +00:00
|
|
|
const fn _has_source<T>(e: T) -> Option<T> {
|
2018-12-29 00:01:58 +00:00
|
|
|
Some(e)
|
|
|
|
}
|
|
|
|
fn _no_source<T, S>(_: T) -> Option<S> {
|
|
|
|
None
|
|
|
|
}
|
2018-12-19 20:52:53 +00:00
|
|
|
|
2018-12-29 00:01:58 +00:00
|
|
|
fn _serialize(e: &impl serde::Serialize, _msg: &str) -> String {
|
2018-12-19 20:52:53 +00:00
|
|
|
serde_json::to_string(e).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-12-29 00:01:58 +00:00
|
|
|
fn _api_error(_: &impl std::any::Any, msg: &str) -> String {
|
2018-12-19 20:52:53 +00:00
|
|
|
let json = json!({
|
|
|
|
"Message": "",
|
|
|
|
"error": "",
|
|
|
|
"error_description": "",
|
|
|
|
"ValidationErrors": {"": [ msg ]},
|
|
|
|
"ErrorModel": {
|
|
|
|
"Message": msg,
|
|
|
|
"Object": "error"
|
|
|
|
},
|
|
|
|
"Object": "error"
|
|
|
|
});
|
2018-12-21 21:08:04 +00:00
|
|
|
_serialize(&json, "")
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Rocket responder impl
|
|
|
|
//
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use rocket::http::{ContentType, Status};
|
|
|
|
use rocket::request::Request;
|
|
|
|
use rocket::response::{self, Responder, Response};
|
|
|
|
|
|
|
|
impl<'r> Responder<'r> for Error {
|
|
|
|
fn respond_to(self, _: &Request) -> response::Result<'r> {
|
2019-12-06 21:19:07 +00:00
|
|
|
match self.error {
|
|
|
|
ErrorKind::EmptyError(_) => {} // Don't print the error in this situation
|
|
|
|
_ => error!(target: "error", "{:#?}", self),
|
|
|
|
};
|
2018-12-19 20:52:53 +00:00
|
|
|
|
2019-03-13 23:17:36 +00:00
|
|
|
let code = Status::from_code(self.error_code).unwrap_or(Status::BadRequest);
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
Response::build()
|
2019-03-13 23:17:36 +00:00
|
|
|
.status(code)
|
2018-12-19 20:52:53 +00:00
|
|
|
.header(ContentType::JSON)
|
2019-12-06 21:19:07 +00:00
|
|
|
.sized_body(Cursor::new(format!("{}", self)))
|
2018-12-19 20:52:53 +00:00
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
//
|
|
|
|
// Error return macros
|
|
|
|
//
|
2018-12-19 20:52:53 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err {
|
|
|
|
($msg:expr) => {{
|
|
|
|
return Err(crate::error::Error::new($msg, $msg));
|
|
|
|
}};
|
|
|
|
($usr_msg:expr, $log_value:expr) => {{
|
|
|
|
return Err(crate::error::Error::new($usr_msg, $log_value));
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:56:26 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_discard {
|
|
|
|
($msg:expr, $data:expr) => {{
|
|
|
|
std::io::copy(&mut $data.open(), &mut std::io::sink()).ok();
|
|
|
|
return Err(crate::error::Error::new($msg, $msg));
|
|
|
|
}};
|
|
|
|
($usr_msg:expr, $log_value:expr, $data:expr) => {{
|
|
|
|
std::io::copy(&mut $data.open(), &mut std::io::sink()).ok();
|
|
|
|
return Err(crate::error::Error::new($usr_msg, $log_value));
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_json {
|
2019-12-07 13:38:32 +00:00
|
|
|
($expr:expr, $log_value:expr) => {{
|
|
|
|
return Err(($log_value, $expr).into());
|
2018-12-19 20:52:53 +00:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_handler {
|
|
|
|
($expr:expr) => {{
|
2019-12-06 21:19:07 +00:00
|
|
|
error!(target: "auth", "Unauthorized Error: {}", $expr);
|
2020-07-19 19:01:31 +00:00
|
|
|
return ::rocket::request::Outcome::Failure((rocket::http::Status::Unauthorized, $expr));
|
2018-12-19 20:52:53 +00:00
|
|
|
}};
|
2019-01-08 15:16:58 +00:00
|
|
|
($usr_msg:expr, $log_value:expr) => {{
|
2019-12-06 21:19:07 +00:00
|
|
|
error!(target: "auth", "Unauthorized Error: {}. {}", $usr_msg, $log_value);
|
2020-07-19 19:01:31 +00:00
|
|
|
return ::rocket::request::Outcome::Failure((rocket::http::Status::Unauthorized, $usr_msg));
|
2019-01-08 15:16:58 +00:00
|
|
|
}};
|
2018-12-19 20:52:53 +00:00
|
|
|
}
|