use std::{
error::Error,
fmt::{self, Display},
future::Future,
};
pub type MyError<T> = Result<T, BError>;
#[derive(Debug)]
pub struct BError {
msg: String,
}
impl BError {
pub fn with_msg(msg: &str) -> Self {
Self {
msg: msg.to_string(),
}
}
pub fn get_msg(&self) -> &str {
&self.msg
}
}
impl Error for BError {}
impl Display for BError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
pub async fn my_err_fn<F, FUT, T>(f: F) -> MyError<T>
where
F: FnOnce() -> FUT,
FUT: Future<Output = MyError<T>>,
{
let res = f().await?;
Ok(res)
}
#[macro_export]
macro_rules! my_error {
($res:expr) => {
match $res {
core::result::Result::Ok(x) => x,
core::result::Result::Err(e) => {
let err_msg = &e.to_string();
return core::result::Result::Err(BError::with_msg(err_msg));
}
}
};
}