2018-12-19 21:52:53 +01:00
|
|
|
//
|
|
|
|
// Error generator macro
|
|
|
|
//
|
2022-11-20 19:15:45 +01:00
|
|
|
use crate::db::models::EventType;
|
2024-07-12 22:33:11 +02:00
|
|
|
use crate::http_client::CustomHttpClientError;
|
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 {
|
2019-02-02 16:47:27 +01:00
|
|
|
( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)? ) => {
|
2019-03-14 00:17:36 +01:00
|
|
|
const BAD_REQUEST: u16 = 400;
|
|
|
|
|
2019-02-08 18:45:07 +01:00
|
|
|
pub enum ErrorKind { $($name( $ty )),+ }
|
2022-11-20 19:15:45 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ErrorEvent { pub event: EventType }
|
|
|
|
pub struct Error { message: String, error: ErrorKind, error_code: u16, event: Option<ErrorEvent> }
|
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 {
|
2022-11-20 19:15:45 +01:00
|
|
|
Error { message: val.0.into(), error: ErrorKind::$name(val.1), error_code: BAD_REQUEST, event: None }
|
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 {
|
2021-11-19 17:50:16 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2018-12-29 01:01:58 +01:00
|
|
|
match &self.error {$(
|
|
|
|
ErrorKind::$name(e) => f.write_str(&$usr_msg_fun(e, &self.message)),
|
2018-12-19 21:52:53 +01:00
|
|
|
)+}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-31 22:18:35 +02:00
|
|
|
use diesel::r2d2::PoolError as R2d2Err;
|
2019-01-20 15:36:33 +01:00
|
|
|
use diesel::result::Error as DieselErr;
|
2020-10-03 22:31:52 +02:00
|
|
|
use diesel::ConnectionError as DieselConErr;
|
2019-01-20 15:36:33 +01:00
|
|
|
use handlebars::RenderError as HbErr;
|
2021-03-27 15:26:32 +01:00
|
|
|
use jsonwebtoken::errors::Error as JwtErr;
|
2021-06-07 23:34:00 +02:00
|
|
|
use lettre::address::AddressError as AddrErr;
|
|
|
|
use lettre::error::Error as LettreErr;
|
|
|
|
use lettre::transport::smtp::Error as SmtpErr;
|
2021-06-26 14:21:58 +02:00
|
|
|
use openssl::error::ErrorStack as SSLErr;
|
2019-02-01 23:39:14 +01:00
|
|
|
use regex::Error as RegexErr;
|
2019-01-20 15:36:33 +01:00
|
|
|
use reqwest::Error as ReqErr;
|
2021-11-07 18:53:39 +01:00
|
|
|
use rocket::error::Error as RocketErr;
|
2019-01-20 15:36:33 +01:00
|
|
|
use serde_json::{Error as SerdeErr, Value};
|
2021-03-27 15:26:32 +01:00
|
|
|
use std::io::Error as IoErr;
|
2019-01-20 15:36:33 +01:00
|
|
|
use std::time::SystemTimeError as TimeErr;
|
2021-06-07 23:34:00 +02:00
|
|
|
use webauthn_rs::error::WebauthnError as WebauthnErr;
|
2019-02-01 23:39:14 +01:00
|
|
|
use yubico::yubicoerror::YubicoError as YubiErr;
|
2020-05-07 00:50:30 +02:00
|
|
|
|
2020-05-03 17:24:51 +02:00
|
|
|
#[derive(Serialize)]
|
2019-03-14 00:17:36 +01:00
|
|
|
pub struct Empty {}
|
|
|
|
|
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! {
|
2019-03-14 00:17:36 +01:00
|
|
|
// Just an empty error
|
2021-06-19 22:02:03 +02:00
|
|
|
Empty(Empty): _no_source, _serialize,
|
2018-12-19 21:52:53 +01:00
|
|
|
// Used to represent err! calls
|
2021-06-19 22:02:03 +02:00
|
|
|
Simple(String): _no_source, _api_error,
|
2024-07-12 22:33:11 +02:00
|
|
|
|
|
|
|
// Used in our custom http client to handle non-global IPs and blocked domains
|
|
|
|
CustomHttpClient(CustomHttpClientError): _has_source, _api_error,
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
// Used for special return values, like 2FA errors
|
2021-06-19 22:02:03 +02:00
|
|
|
Json(Value): _no_source, _serialize,
|
|
|
|
Db(DieselErr): _has_source, _api_error,
|
|
|
|
R2d2(R2d2Err): _has_source, _api_error,
|
|
|
|
Serde(SerdeErr): _has_source, _api_error,
|
|
|
|
JWt(JwtErr): _has_source, _api_error,
|
|
|
|
Handlebars(HbErr): _has_source, _api_error,
|
2021-11-05 19:18:54 +01:00
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
Io(IoErr): _has_source, _api_error,
|
|
|
|
Time(TimeErr): _has_source, _api_error,
|
|
|
|
Req(ReqErr): _has_source, _api_error,
|
|
|
|
Regex(RegexErr): _has_source, _api_error,
|
|
|
|
Yubico(YubiErr): _has_source, _api_error,
|
|
|
|
|
|
|
|
Lettre(LettreErr): _has_source, _api_error,
|
|
|
|
Address(AddrErr): _has_source, _api_error,
|
|
|
|
Smtp(SmtpErr): _has_source, _api_error,
|
2021-06-25 20:49:44 +02:00
|
|
|
OpenSSL(SSLErr): _has_source, _api_error,
|
2021-11-07 18:53:39 +01:00
|
|
|
Rocket(RocketErr): _has_source, _api_error,
|
2021-06-19 22:02:03 +02:00
|
|
|
|
|
|
|
DieselCon(DieselConErr): _has_source, _api_error,
|
|
|
|
Webauthn(WebauthnErr): _has_source, _api_error,
|
2018-12-29 01:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Error {
|
2021-11-19 17:50:16 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2018-12-29 01:01:58 +01:00
|
|
|
match self.source() {
|
|
|
|
Some(e) => write!(f, "{}.\n[CAUSE] {:#?}", self.message, e),
|
2019-12-07 14:38:32 +01:00
|
|
|
None => match self.error {
|
2021-06-19 22:02:03 +02:00
|
|
|
ErrorKind::Empty(_) => Ok(()),
|
|
|
|
ErrorKind::Simple(ref s) => {
|
2019-12-07 14:38:32 +01:00
|
|
|
if &self.message == s {
|
|
|
|
write!(f, "{}", self.message)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}. {}", self.message, s)
|
|
|
|
}
|
2019-12-27 18:37:14 +01:00
|
|
|
}
|
2021-06-19 22:02:03 +02:00
|
|
|
ErrorKind::Json(_) => write!(f, "{}", self.message),
|
2019-12-07 14:38:32 +01:00
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2018-12-29 01:01:58 +01:00
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2019-03-14 00:17:36 +01:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
Empty {}.into()
|
|
|
|
}
|
|
|
|
|
2021-12-26 12:40:12 +01:00
|
|
|
#[must_use]
|
2018-12-29 01:01:58 +01:00
|
|
|
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-03-14 00:17:36 +01:00
|
|
|
|
2021-12-26 12:40:12 +01:00
|
|
|
#[must_use]
|
2020-05-03 17:24:51 +02:00
|
|
|
pub const fn with_code(mut self, code: u16) -> Self {
|
2019-03-14 00:17:36 +01:00
|
|
|
self.error_code = code;
|
|
|
|
self
|
|
|
|
}
|
2022-11-20 19:15:45 +01:00
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn with_event(mut self, event: ErrorEvent) -> Self {
|
|
|
|
self.event = Some(event);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_event(&self) -> &Option<ErrorEvent> {
|
|
|
|
&self.event
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:40:03 +02: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 17:24:51 +02:00
|
|
|
const fn _has_source<T>(e: T) -> Option<T> {
|
2018-12-29 01:01:58 +01:00
|
|
|
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!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"message": msg,
|
2018-12-19 21:52:53 +01:00
|
|
|
"error": "",
|
|
|
|
"error_description": "",
|
2024-06-23 21:31:02 +02:00
|
|
|
"validationErrors": {"": [ msg ]},
|
|
|
|
"errorModel": {
|
|
|
|
"message": msg,
|
|
|
|
"object": "error"
|
2018-12-19 21:52:53 +01:00
|
|
|
},
|
2024-06-23 21:31:02 +02:00
|
|
|
"exceptionMessage": null,
|
|
|
|
"exceptionStackTrace": null,
|
|
|
|
"innerExceptionMessage": null,
|
|
|
|
"object": "error"
|
2018-12-19 21:52:53 +01:00
|
|
|
});
|
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};
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
impl<'r> Responder<'r, 'static> for Error {
|
2021-11-19 17:50:16 +01:00
|
|
|
fn respond_to(self, _: &Request<'_>) -> response::Result<'static> {
|
2019-12-06 22:19:07 +01:00
|
|
|
match self.error {
|
2021-06-19 22:02:03 +02:00
|
|
|
ErrorKind::Empty(_) => {} // Don't print the error in this situation
|
|
|
|
ErrorKind::Simple(_) => {} // Don't print the error in this situation
|
2019-12-06 22:19:07 +01:00
|
|
|
_ => error!(target: "error", "{:#?}", self),
|
|
|
|
};
|
2018-12-19 21:52:53 +01:00
|
|
|
|
2019-03-14 00:17:36 +01:00
|
|
|
let code = Status::from_code(self.error_code).unwrap_or(Status::BadRequest);
|
2021-11-07 18:53:39 +01:00
|
|
|
let body = self.to_string();
|
|
|
|
Response::build().status(code).header(ContentType::JSON).sized_body(Some(body.len()), Cursor::new(body)).ok()
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) => {{
|
2020-11-21 23:12:25 +01:00
|
|
|
error!("{}", $msg);
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($msg, $msg));
|
2018-12-19 21:52:53 +01:00
|
|
|
}};
|
2022-11-20 19:15:45 +01:00
|
|
|
($msg:expr, ErrorEvent $err_event:tt) => {{
|
|
|
|
error!("{}", $msg);
|
|
|
|
return Err($crate::error::Error::new($msg, $msg).with_event($crate::error::ErrorEvent $err_event));
|
|
|
|
}};
|
2018-12-19 21:52:53 +01:00
|
|
|
($usr_msg:expr, $log_value:expr) => {{
|
2020-11-21 23:12:25 +01:00
|
|
|
error!("{}. {}", $usr_msg, $log_value);
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($usr_msg, $log_value));
|
2018-12-19 21:52:53 +01:00
|
|
|
}};
|
2022-11-20 19:15:45 +01:00
|
|
|
($usr_msg:expr, $log_value:expr, ErrorEvent $err_event:tt) => {{
|
|
|
|
error!("{}. {}", $usr_msg, $log_value);
|
|
|
|
return Err($crate::error::Error::new($usr_msg, $log_value).with_event($crate::error::ErrorEvent $err_event));
|
|
|
|
}};
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 19:15:45 +01:00
|
|
|
#[macro_export]
|
2021-09-24 18:27:52 +02:00
|
|
|
macro_rules! err_silent {
|
|
|
|
($msg:expr) => {{
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($msg, $msg));
|
2021-09-24 18:27:52 +02:00
|
|
|
}};
|
|
|
|
($usr_msg:expr, $log_value:expr) => {{
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($usr_msg, $log_value));
|
2021-09-24 18:27:52 +02:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:56:26 +01:00
|
|
|
#[macro_export]
|
2021-03-14 23:35:55 +01:00
|
|
|
macro_rules! err_code {
|
2022-11-20 19:15:45 +01:00
|
|
|
($msg:expr, $err_code:expr) => {{
|
2021-03-14 23:35:55 +01:00
|
|
|
error!("{}", $msg);
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($msg, $msg).with_code($err_code));
|
2021-03-14 23:35:55 +01:00
|
|
|
}};
|
2022-11-20 19:15:45 +01:00
|
|
|
($usr_msg:expr, $log_value:expr, $err_code:expr) => {{
|
2021-03-14 23:35:55 +01:00
|
|
|
error!("{}. {}", $usr_msg, $log_value);
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($usr_msg, $log_value).with_code($err_code));
|
2021-03-14 23:35:55 +01:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2020-02-17 22:56:26 +01:00
|
|
|
macro_rules! err_discard {
|
|
|
|
($msg:expr, $data:expr) => {{
|
|
|
|
std::io::copy(&mut $data.open(), &mut std::io::sink()).ok();
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($msg, $msg));
|
2020-02-17 22:56:26 +01:00
|
|
|
}};
|
|
|
|
($usr_msg:expr, $log_value:expr, $data:expr) => {{
|
|
|
|
std::io::copy(&mut $data.open(), &mut std::io::sink()).ok();
|
2022-05-04 21:13:05 +02:00
|
|
|
return Err($crate::error::Error::new($usr_msg, $log_value));
|
2020-02-17 22:56:26 +01:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_json {
|
2019-12-07 14:38:32 +01:00
|
|
|
($expr:expr, $log_value:expr) => {{
|
|
|
|
return Err(($log_value, $expr).into());
|
2018-12-19 21:52:53 +01:00
|
|
|
}};
|
2022-11-20 19:15:45 +01:00
|
|
|
($expr:expr, $log_value:expr, $err_event:expr, ErrorEvent) => {{
|
|
|
|
return Err(($log_value, $expr).into().with_event($err_event));
|
|
|
|
}};
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_handler {
|
|
|
|
($expr:expr) => {{
|
2019-12-06 22:19:07 +01:00
|
|
|
error!(target: "auth", "Unauthorized Error: {}", $expr);
|
2023-11-15 10:41:14 +01:00
|
|
|
return ::rocket::request::Outcome::Error((rocket::http::Status::Unauthorized, $expr));
|
2018-12-19 21:52:53 +01:00
|
|
|
}};
|
2019-01-08 16:16:58 +01:00
|
|
|
($usr_msg:expr, $log_value:expr) => {{
|
2019-12-06 22:19:07 +01:00
|
|
|
error!(target: "auth", "Unauthorized Error: {}. {}", $usr_msg, $log_value);
|
2023-11-15 10:41:14 +01:00
|
|
|
return ::rocket::request::Outcome::Error((rocket::http::Status::Unauthorized, $usr_msg));
|
2019-01-08 16:16:58 +01:00
|
|
|
}};
|
2018-12-19 21:52:53 +01:00
|
|
|
}
|