Skip to main content

surrealml_core/errors/
error.rs

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