use serde::{Deserialize, Serialize};
use thiserror::Error;
use std::fmt;
#[macro_export]
macro_rules! safe_eject {
($e:expr, $err_status:expr, $msg:expr) => {
$e.map_err(|x| {let file_track = format!("{}:{}", file!(), line!()); let formatted_error = format!("{} => {}", file_track, x.to_string()); SurrealError::new(formatted_error, $err_status)})?
};
($e:expr, $err_status:expr) => {
$e.map_err(|x| {let file_track = format!("{}:{}", file!(), line!()); let formatted_error = format!("{} => {}", file_track, x.to_string()); SurrealError::new(formatted_error, $err_status)})?
};
}
#[macro_export]
macro_rules! safe_eject_internal {
($e:expr, $err_status:expr, $msg:expr) => {
$e.map_err(|x| {let file_track = format!("{}:{}", file!(), line!()); let formatted_error = format!("{} => {}", file_track, x.to_string()); SurrealError::new(formatted_error, SurrealErrorStatus::Unknown)})?
};
($e:expr) => {
$e.map_err(|x| {let file_track = format!("{}:{}", file!(), line!()); let formatted_error = format!("{} => {}", file_track, x.to_string()); SurrealError::new(formatted_error, SurrealErrorStatus::Unknown)})?
};
}
#[macro_export]
macro_rules! safe_eject_option {
($check:expr) => {
match $check {Some(x) => x, None => {let file_track = format!("{}:{}", file!(), line!());let message = format!("{}=>The value is not found", file_track);return Err(SurrealError::new(message, SurrealErrorStatus::NotFound))}}
};
}
#[derive(Error, Debug, Serialize, Deserialize, PartialEq)]
pub enum SurrealErrorStatus {
#[error("not found")]
NotFound,
#[error("You are forbidden to access resource")]
Forbidden,
#[error("Unknown Internal Error")]
Unknown,
#[error("Bad Request")]
BadRequest,
#[error("Conflict")]
Conflict,
#[error("Unauthorized")]
Unauthorized
}
#[derive(Serialize, Deserialize, Debug, Error)]
pub struct SurrealError {
pub message: String,
pub status: SurrealErrorStatus
}
impl SurrealError {
pub fn new(message: String, status: SurrealErrorStatus) -> Self {
SurrealError {
message,
status
}
}
}
impl fmt::Display for SurrealError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}