surrealml_core/errors/
error.rs

1//! Custom error that can be attached to a web framework to automcatically result in a http response,
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4use std::fmt;
5
6
7#[macro_export]
8macro_rules! safe_eject {
9    // Match when the optional string is provided
10    ($e:expr, $err_status:expr, $msg:expr) => {
11        $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)})?
12    };
13    // Match when the optional string is not provided
14    ($e:expr, $err_status:expr) => {
15        $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)})?
16    };
17}
18
19
20#[macro_export]
21macro_rules! safe_eject_internal {
22    // Match when the optional string is provided
23    ($e:expr, $err_status:expr, $msg:expr) => {
24        $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)})?
25    };
26    // Match when the optional string is not provided
27    ($e:expr) => {
28        $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)})?
29    };
30}
31
32
33#[macro_export]
34macro_rules! safe_eject_option {
35    ($check:expr) => {
36        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))}}
37    };
38}
39
40
41/// The status of the custom error.
42/// 
43/// # Fields
44/// * `NotFound` - The request was not found.
45/// * `Forbidden` - You are forbidden to access.
46/// * `Unknown` - An unknown internal error occurred.
47/// * `BadRequest` - The request was bad.
48/// * `Conflict` - The request conflicted with the current state of the server.
49#[derive(Error, Debug, Serialize, Deserialize, PartialEq)]
50pub enum SurrealErrorStatus {
51    #[error("not found")]
52    NotFound,
53    #[error("You are forbidden to access resource")]
54    Forbidden,
55    #[error("Unknown Internal Error")]
56    Unknown,
57    #[error("Bad Request")]
58    BadRequest,
59    #[error("Conflict")]
60    Conflict,
61    #[error("Unauthorized")]
62    Unauthorized
63}
64
65
66/// The custom error that the web framework will construct into a HTTP response.
67/// 
68/// # Fields
69/// * `message` - The message of the error.
70/// * `status` - The status of the error.
71#[derive(Serialize, Deserialize, Debug, Error)]
72pub struct SurrealError {
73    pub message: String,
74    pub status: SurrealErrorStatus
75}
76
77
78impl SurrealError {
79
80    /// Create a new custom error.
81    /// 
82    /// # Arguments
83    /// * `message` - The message of the error.
84    /// * `status` - The status of the error.
85    /// 
86    /// # Returns
87    /// A new custom error.
88    pub fn new(message: String, status: SurrealErrorStatus) -> Self {
89        SurrealError {
90            message,
91            status
92        }
93    }
94}
95
96
97impl fmt::Display for SurrealError {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(f, "{}", self.message)
100    }
101}