2018-12-19 15:52:53 -05:00
|
|
|
//
|
|
|
|
// Error generator macro
|
|
|
|
//
|
|
|
|
macro_rules! make_error {
|
|
|
|
( $struct:ident; $( $name:ident ( $ty:ty, _): $show_cause:expr, $usr_msg_fun:expr ),+ $(,)* ) => {
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[allow(unused_variables, dead_code)]
|
|
|
|
pub enum $struct {
|
|
|
|
$($name( $ty, String )),+
|
|
|
|
}
|
|
|
|
$(impl From<$ty> for $struct {
|
|
|
|
fn from(err: $ty) -> Self {
|
|
|
|
$struct::$name(err, String::from(stringify!($name)))
|
|
|
|
}
|
|
|
|
})+
|
|
|
|
$(impl From<($ty, String)> for $struct {
|
|
|
|
fn from(err: ($ty, String)) -> Self {
|
|
|
|
$struct::$name(err.0, err.1)
|
|
|
|
}
|
|
|
|
})+
|
|
|
|
impl $struct {
|
|
|
|
pub fn with_msg<M: Into<String>>(self, msg: M) -> Self {
|
|
|
|
match self {$(
|
|
|
|
$struct::$name(e, _) => $struct::$name(e, msg.into()),
|
|
|
|
)+}
|
|
|
|
}
|
2018-12-21 16:27:35 -05:00
|
|
|
// Prints the log message and returns the user message
|
2018-12-19 15:52:53 -05:00
|
|
|
pub fn display_error(self) -> String {
|
|
|
|
match &self {$(
|
|
|
|
$struct::$name(e, s) => {
|
|
|
|
let log_msg = format!("{}. {}", &s, &e);
|
|
|
|
|
|
|
|
error!("{}", log_msg);
|
|
|
|
if $show_cause {
|
2018-12-21 16:27:35 -05:00
|
|
|
error!("[CAUSE] {:#?}", e);
|
2018-12-19 15:52:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$usr_msg_fun(e, s)
|
|
|
|
},
|
|
|
|
)+}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:08:04 -05:00
|
|
|
use diesel::result::Error as DieselError;
|
|
|
|
use jsonwebtoken::errors::Error as JwtError;
|
|
|
|
use serde_json::{Error as SerError, Value};
|
2018-12-19 15:52:53 -05:00
|
|
|
use u2f::u2ferror::U2fError as U2fErr;
|
2018-12-23 16:37:02 -05:00
|
|
|
use std::io::Error as IOError;
|
2018-12-19 15:52:53 -05:00
|
|
|
|
|
|
|
// Error struct
|
|
|
|
// Each variant has two elements, the first is an error of different types, used for logging purposes
|
|
|
|
// The second is a String, and it's contents are displayed to the user when the error occurs. Inside the macro, this is represented as _
|
2018-12-21 16:08:04 -05:00
|
|
|
//
|
2018-12-19 15:52:53 -05:00
|
|
|
// After the variant itself, there are two expressions. The first one is a bool to indicate whether the error cause will be printed to the log.
|
|
|
|
// The second one contains the function used to obtain the response sent to the client
|
|
|
|
make_error! {
|
|
|
|
Error;
|
|
|
|
// Used to represent err! calls
|
|
|
|
SimpleError(String, _): false, _api_error,
|
|
|
|
// Used for special return values, like 2FA errors
|
|
|
|
JsonError(Value, _): false, _serialize,
|
|
|
|
DbError(DieselError, _): true, _api_error,
|
|
|
|
U2fError(U2fErr, _): true, _api_error,
|
|
|
|
SerdeError(SerError, _): true, _api_error,
|
2018-12-21 16:08:04 -05:00
|
|
|
JWTError(JwtError, _): true, _api_error,
|
2018-12-23 16:37:02 -05:00
|
|
|
IoErrror(IOError, _): true, _api_error,
|
2018-12-19 15:52:53 -05:00
|
|
|
//WsError(ws::Error, _): true, _api_error,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
pub fn new<M: Into<String>, N: Into<String>>(usr_msg: M, log_msg: N) -> Self {
|
|
|
|
Error::SimpleError(log_msg.into(), usr_msg.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MapResult<S, E> {
|
2018-12-21 16:08:04 -05:00
|
|
|
fn map_res(self, msg: &str) -> Result<S, E>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, E: Into<Error>> MapResult<S, Error> for Result<S, E> {
|
|
|
|
fn map_res(self, msg: &str) -> Result<S, Error> {
|
|
|
|
self.map_err(Into::into).map_err(|e| e.with_msg(msg))
|
|
|
|
}
|
2018-12-19 15:52:53 -05:00
|
|
|
}
|
|
|
|
|
2018-12-21 16:08:04 -05:00
|
|
|
impl<E: Into<Error>> MapResult<(), Error> for Result<usize, E> {
|
2018-12-19 15:52:53 -05:00
|
|
|
fn map_res(self, msg: &str) -> Result<(), Error> {
|
2018-12-21 16:08:04 -05:00
|
|
|
self.and(Ok(())).map_res(msg)
|
2018-12-19 15:52:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::any::Any;
|
|
|
|
|
2018-12-21 16:08:04 -05:00
|
|
|
fn _serialize(e: &impl Serialize, _msg: &str) -> String {
|
2018-12-19 15:52:53 -05:00
|
|
|
serde_json::to_string(e).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _api_error(_: &impl Any, msg: &str) -> String {
|
|
|
|
let json = json!({
|
|
|
|
"Message": "",
|
|
|
|
"error": "",
|
|
|
|
"error_description": "",
|
|
|
|
"ValidationErrors": {"": [ msg ]},
|
|
|
|
"ErrorModel": {
|
|
|
|
"Message": msg,
|
|
|
|
"Object": "error"
|
|
|
|
},
|
|
|
|
"Object": "error"
|
|
|
|
});
|
|
|
|
|
2018-12-21 16:08:04 -05:00
|
|
|
_serialize(&json, "")
|
2018-12-19 15:52:53 -05: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> {
|
|
|
|
let usr_msg = self.display_error();
|
|
|
|
|
|
|
|
Response::build()
|
|
|
|
.status(Status::BadRequest)
|
|
|
|
.header(ContentType::JSON)
|
|
|
|
.sized_body(Cursor::new(usr_msg))
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Error return macros
|
|
|
|
///
|
|
|
|
#[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));
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_json {
|
|
|
|
($expr:expr) => {{
|
|
|
|
return Err(crate::error::Error::from($expr));
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_handler {
|
|
|
|
($expr:expr) => {{
|
|
|
|
return rocket::Outcome::Failure((rocket::http::Status::Unauthorized, $expr));
|
|
|
|
}};
|
|
|
|
}
|