Files
tuliprox/shared/src/error/tuliprox_error.rs
T

128 lines
3.1 KiB
Rust
Raw Normal View History

use std::error::Error;
use std::fmt::{Display, Formatter, Result};
2023-10-13 13:59:22 +02:00
#[macro_export]
2023-10-19 18:56:08 +02:00
macro_rules! get_errors_notify_message {
2023-10-13 13:59:22 +02:00
($errors:expr, $size:expr) => {
if $errors.is_empty() {
None
} else {
2025-01-17 18:32:17 +01:00
let text = $errors
.iter()
2025-08-13 23:07:21 +02:00
.filter(|&err| err.kind == $crate::error::TuliproxErrorKind::Notify)
2025-01-17 18:32:17 +01:00
.map(|err| err.message.as_str())
.collect::<Vec<&str>>()
2025-01-31 23:08:23 +01:00
.join("\r\n");
2025-01-17 18:32:17 +01:00
if $size > 0 && text.len() > std::cmp::max($size - 3, 3) {
2023-10-13 13:59:22 +02:00
Some(format!("{}...", text.get(0..$size).unwrap()))
} else {
Some(text)
}
}
2025-01-17 18:32:17 +01:00
};
2023-10-13 13:59:22 +02:00
}
2025-02-05 20:01:49 +01:00
pub use get_errors_notify_message;
2024-12-23 11:30:33 +01:00
#[macro_export]
macro_rules! notify_err {
($text:expr) => {
2025-08-13 23:07:21 +02:00
$crate::error::TuliproxError::new($crate::error::TuliproxErrorKind::Notify, $text)
2024-12-23 11:30:33 +01:00
};
}
2025-02-05 20:01:49 +01:00
pub use notify_err;
2024-12-23 11:30:33 +01:00
#[macro_export]
macro_rules! info_err {
($text:expr) => {
2025-08-13 23:07:21 +02:00
$crate::error::TuliproxError::new($crate::error::TuliproxErrorKind::Info, $text)
2024-12-23 11:30:33 +01:00
};
}
2025-02-05 20:01:49 +01:00
pub use info_err;
#[macro_export]
2025-05-10 18:07:41 +02:00
macro_rules! create_tuliprox_error {
2025-02-05 20:01:49 +01:00
($kind: expr, $($arg:tt)*) => {
2025-08-13 23:07:21 +02:00
$crate::error::TuliproxError::new($kind, format!($($arg)*))
2025-02-05 20:01:49 +01:00
}
}
2025-05-10 18:07:41 +02:00
pub use create_tuliprox_error;
2025-02-05 20:01:49 +01:00
#[macro_export]
2025-05-10 18:07:41 +02:00
macro_rules! create_tuliprox_error_result {
2025-02-05 20:01:49 +01:00
($kind: expr, $($arg:tt)*) => {
2025-08-13 23:07:21 +02:00
Err($crate::error::TuliproxError::new($kind, format!($($arg)*)))
2025-02-05 20:01:49 +01:00
}
}
2025-05-10 18:07:41 +02:00
pub use create_tuliprox_error_result;
2025-02-05 20:01:49 +01:00
#[macro_export]
2025-05-10 18:07:41 +02:00
macro_rules! handle_tuliprox_error_result_list {
2025-02-05 20:01:49 +01:00
($kind:expr, $result: expr) => {
let errors = $result
.filter_map(|result| {
if let Err(err) = result {
Some(err.to_string())
} else {
None
}
})
.collect::<Vec<String>>();
if !&errors.is_empty() {
2025-08-13 23:07:21 +02:00
return Err($crate::error::TuliproxError::new($kind, errors.join("\n")));
2025-02-05 20:01:49 +01:00
}
}
}
2025-05-10 18:07:41 +02:00
pub use handle_tuliprox_error_result_list;
2025-02-05 20:01:49 +01:00
#[macro_export]
2025-05-10 18:07:41 +02:00
macro_rules! handle_tuliprox_error_result {
2025-02-05 20:01:49 +01:00
($kind:expr, $result: expr) => {
if let Err(err) = $result {
2025-08-13 23:07:21 +02:00
return Err($crate::error::TuliproxError::new($kind, err.to_string()));
2025-02-05 20:01:49 +01:00
}
}
}
2025-05-10 18:07:41 +02:00
pub use handle_tuliprox_error_result;
2025-02-05 20:01:49 +01:00
2024-12-23 11:30:33 +01:00
2025-05-26 13:57:29 +02:00
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2025-05-13 21:50:08 +02:00
pub enum TuliproxErrorKind {
2023-10-13 13:59:22 +02:00
// do not send with messaging
Info,
Notify, // send with messaging
}
#[derive(Debug)]
2025-05-13 21:50:08 +02:00
pub struct TuliproxError {
pub kind: TuliproxErrorKind,
2023-10-13 13:59:22 +02:00
pub message: String,
}
2025-05-13 21:50:08 +02:00
impl TuliproxError {
pub const fn new(kind: TuliproxErrorKind, message: String) -> Self {
2025-01-17 18:32:17 +01:00
Self { kind, message }
}
}
2025-05-13 21:50:08 +02:00
impl Display for TuliproxError {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "Tuliprox error: {}", self.message)
}
}
2025-05-13 21:50:08 +02:00
impl Error for TuliproxError {}
2025-01-17 18:32:17 +01:00
pub fn to_io_error<E>(err: E) -> std::io::Error
2025-02-25 19:07:26 +01:00
where
E: std::error::Error,
2025-05-16 14:11:33 +02:00
{ std::io::Error::other(err.to_string()) }
2025-01-17 18:32:17 +01:00
pub fn str_to_io_error(err: &str) -> std::io::Error {
2025-05-16 14:11:33 +02:00
std::io::Error::other(err.to_string())
2025-01-17 18:32:17 +01:00
}