2018-12-19 21:52:53 +01:00
|
|
|
//
|
|
|
|
// Error generator macro
|
|
|
|
//
|
2018-12-29 01:01:58 +01:00
|
|
|
use std::error::Error as StdError;
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
macro_rules! make_error {
|
2018-12-29 01:01:58 +01:00
|
|
|
( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)* ) => {
|
|
|
|
#[derive(Display)]
|
|
|
|
enum ErrorKind { $($name( $ty )),+ }
|
|
|
|
pub struct Error { message: String, error: ErrorKind }
|
2018-12-30 23:34:31 +01:00
|
|
|
|
2018-12-29 01:01:58 +01:00
|
|
|
$(impl From<$ty> for Error {
|
|
|
|
fn from(err: $ty) -> Self { Error::from((stringify!($name), err)) }
|
2018-12-19 21:52:53 +01:00
|
|
|
})+
|
2018-12-29 01:01:58 +01:00
|
|
|
$(impl<S: Into<String>> From<(S, $ty)> for Error {
|
|
|
|
fn from(val: (S, $ty)) -> Self {
|
|
|
|
Error { message: val.0.into(), error: ErrorKind::$name(val.1) }
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
})+
|
2018-12-29 01:01:58 +01:00
|
|
|
impl StdError for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
|
|
|
match &self.error {$( ErrorKind::$name(e) => $src_fn(e), )+}
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
2018-12-29 01:01:58 +01: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 21:52:53 +01:00
|
|
|
)+}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-21 22:08:04 +01:00
|
|
|
use diesel::result::Error as DieselError;
|
|
|
|
use jsonwebtoken::errors::Error as JwtError;
|
|
|
|
use serde_json::{Error as SerError, Value};
|
2018-12-23 22:37:02 +01:00
|
|
|
use std::io::Error as IOError;
|
2018-12-29 01:01:58 +01:00
|
|
|
use u2f::u2ferror::U2fError as U2fErr;
|
2018-12-19 21:52:53 +01:00
|
|
|
|
|
|
|
// Error struct
|
2018-12-29 01:01:58 +01:00
|
|
|
// Contains a String error message, meant for the user and an enum variant, with an error of different types.
|
2018-12-21 22:08:04 +01:00
|
|
|
//
|
2018-12-29 01:01:58 +01: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 21:52:53 +01:00
|
|
|
// The second one contains the function used to obtain the response sent to the client
|
|
|
|
make_error! {
|
|
|
|
// Used to represent err! calls
|
2018-12-29 01:01:58 +01:00
|
|
|
SimpleError(String): _no_source, _api_error,
|
2018-12-19 21:52:53 +01:00
|
|
|
// Used for special return values, like 2FA errors
|
2018-12-29 01:01:58 +01:00
|
|
|
JsonError(Value): _no_source, _serialize,
|
|
|
|
DbError(DieselError): _has_source, _api_error,
|
|
|
|
U2fError(U2fErr): _has_source, _api_error,
|
|
|
|
SerdeError(SerError): _has_source, _api_error,
|
|
|
|
JWTError(JwtError): _has_source, _api_error,
|
|
|
|
IoErrror(IOError): _has_source, _api_error,
|
|
|
|
//WsError(ws::Error): _has_source, _api_error,
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
None => write!(f, "{}. {}", self.message, self.error),
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
pub fn new<M: Into<String>, N: Into<String>>(usr_msg: M, log_msg: N) -> Self {
|
2018-12-29 01:01:58 +01:00
|
|
|
(usr_msg, log_msg.into()).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_msg<M: Into<String>>(mut self, msg: M) -> Self {
|
|
|
|
self.message = msg.into();
|
|
|
|
self
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 00:25:38 +01:00
|
|
|
pub trait MapResult<S> {
|
|
|
|
fn map_res(self, msg: &str) -> Result<S, Error>;
|
2018-12-21 22:08:04 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 00:25:38 +01:00
|
|
|
impl<S, E: Into<Error>> MapResult<S> for Result<S, E> {
|
2018-12-21 22:08:04 +01:00
|
|
|
fn map_res(self, msg: &str) -> Result<S, Error> {
|
2018-12-29 01:01:58 +01:00
|
|
|
self.map_err(|e| e.into().with_msg(msg))
|
2018-12-21 22:08:04 +01:00
|
|
|
}
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 00:25:38 +01:00
|
|
|
impl<E: Into<Error>> MapResult<()> for Result<usize, E> {
|
2018-12-19 21:52:53 +01:00
|
|
|
fn map_res(self, msg: &str) -> Result<(), Error> {
|
2018-12-21 22:08:04 +01:00
|
|
|
self.and(Ok(())).map_res(msg)
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 01:01:58 +01:00
|
|
|
fn _has_source<T>(e: T) -> Option<T> {
|
|
|
|
Some(e)
|
|
|
|
}
|
|
|
|
fn _no_source<T, S>(_: T) -> Option<S> {
|
|
|
|
None
|
|
|
|
}
|
2018-12-19 21:52:53 +01:00
|
|
|
|
2018-12-29 01:01:58 +01:00
|
|
|
fn _serialize(e: &impl serde::Serialize, _msg: &str) -> String {
|
2018-12-19 21:52:53 +01:00
|
|
|
serde_json::to_string(e).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-12-29 01:01:58 +01:00
|
|
|
fn _api_error(_: &impl std::any::Any, msg: &str) -> String {
|
2018-12-19 21:52:53 +01:00
|
|
|
let json = json!({
|
|
|
|
"Message": "",
|
|
|
|
"error": "",
|
|
|
|
"error_description": "",
|
|
|
|
"ValidationErrors": {"": [ msg ]},
|
|
|
|
"ErrorModel": {
|
|
|
|
"Message": msg,
|
|
|
|
"Object": "error"
|
|
|
|
},
|
|
|
|
"Object": "error"
|
|
|
|
});
|
2018-12-21 22:08:04 +01:00
|
|
|
_serialize(&json, "")
|
2018-12-19 21:52:53 +01: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> {
|
2018-12-29 01:01:58 +01:00
|
|
|
let usr_msg = format!("{}", self);
|
|
|
|
error!("{:#?}", self);
|
2018-12-19 21:52:53 +01:00
|
|
|
|
|
|
|
Response::build()
|
|
|
|
.status(Status::BadRequest)
|
|
|
|
.header(ContentType::JSON)
|
|
|
|
.sized_body(Cursor::new(usr_msg))
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
//
|
|
|
|
// Error return macros
|
|
|
|
//
|
2018-12-19 21:52:53 +01: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));
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_json {
|
|
|
|
($expr:expr) => {{
|
|
|
|
return Err(crate::error::Error::from($expr));
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_handler {
|
|
|
|
($expr:expr) => {{
|
2019-01-08 16:16:58 +01:00
|
|
|
error!("Unauthorized Error: {}", $expr);
|
2018-12-19 21:52:53 +01:00
|
|
|
return rocket::Outcome::Failure((rocket::http::Status::Unauthorized, $expr));
|
|
|
|
}};
|
2019-01-08 16:16:58 +01:00
|
|
|
($usr_msg:expr, $log_value:expr) => {{
|
|
|
|
error!("Unauthorized Error: {}. {}", $usr_msg, $log_value);
|
|
|
|
return rocket::Outcome::Failure((rocket::http::Status::Unauthorized, $usr_msg));
|
|
|
|
}};
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|