use std::fmt;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub struct Error {
pub msg: String,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
impl Default for Error {
fn default() -> Self {
Error {
msg: "".to_string(),
source: None,
}
}
}
impl Error {
pub fn new(msg: &str) -> Self {
Error {
msg: msg.to_string(),
source: None,
}
}
pub fn with_source(msg: &str, source: Box<dyn std::error::Error + Send + Sync>) -> Self {
Error {
msg: msg.to_string(),
source: Some(source),
}
}
}
impl From<config::ConfigError> for Error {
fn from(err: config::ConfigError) -> Self {
Error {
msg: String::from("Config Error"),
source: Some(Box::new(err)),
}
}
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(_err: std::sync::PoisonError<T>) -> Self {
Error {
msg: String::from("Poison Error"),
source: None,
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error {
msg: String::from("IO Error"),
source: Some(Box::new(err)),
}
}
}
impl From<clap::Error> for Error {
fn from(err: clap::Error) -> Self {
Error {
msg: String::from("Clap Error"),
source: Some(Box::new(err)),
}
}
}
impl From<log::SetLoggerError> for Error {
fn from(err: log::SetLoggerError) -> Self {
Error {
msg: String::from("Logger Error"),
source: Some(Box::new(err)),
}
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Error {
msg: String::from("FromUtf8Error"),
source: Some(Box::new(err)),
}
}
}
impl From<async_openai::error::OpenAIError> for Error {
fn from(err: async_openai::error::OpenAIError) -> Self {
Error {
msg: String::from("OpenAIError"),
source: Some(Box::new(err)),
}
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Error {
msg: String::from("ParseIntError"),
source: Some(Box::new(err)),
}
}
}