use std::{error::Error, fmt};
pub use chrono::{DateTime, Local};
use colored::Colorize;
#[derive(Debug, Clone)]
pub struct Exception {
pub time: DateTime<Local>,
pub msg: String,
pub file: String,
pub line: u32,
}
impl Exception {
pub fn new<S>(msg: S, file: S, line: u32) -> Exception
where
S: ToString,
{
Exception {
time: Local::now(),
msg: msg.to_string(),
file: file.to_string(),
line,
}
}
pub fn pack<E,S>(err: E, file: S, line: u32) -> Exception
where
E: Error,
S: ToString
{
Exception {
time: Local::now(),
msg: err.to_string(),
file: file.to_string(),
line,
}
}
pub fn report(&self) {
eprint!("{}:{}", "error".red().bold(), self)
}
}
impl Error for Exception {}
impl fmt::Display for Exception {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"\n\t time: {},\n\t msg: {}\n\twhere: {}\n",
self.time,
self.msg,
format!("{}:{}", self.file, self.line)
)
}
}
impl From<std::io::Error> for Exception {
fn from(value: std::io::Error) -> Self {
Exception::pack(value, "<Can not captured>", 0)
}
}