use std::path::PathBuf;
use miette::{Diagnostic, GraphicalReportHandler, GraphicalTheme};
use thiserror::Error as ThisError;
#[derive(Debug, ThisError, Diagnostic)]
#[error(transparent)]
#[diagnostic(transparent)]
pub enum Error {
Connection(#[from] crate::conn::error::Error),
ReadQueries(#[from] crate::read_queries::error::Error),
ParseQueries(#[from] Box<crate::parser::error::Error>),
ValidateQueries(#[from] Box<crate::validation::error::Error>),
Container(#[from] crate::container::error::Error),
PrepareQueries(#[from] Box<crate::prepare_queries::error::Error>),
LoadSchema(#[from] Box<crate::load_schema::error::Error>),
PersistCrate(#[from] PersistError),
Config(#[from] crate::config::ConfigError),
}
impl Error {
#[must_use]
pub fn report(self) -> String {
let mut buff = String::new();
if GraphicalReportHandler::new()
.with_theme(GraphicalTheme::unicode_nocolor())
.render_report(&mut buff, &self)
.is_err()
{
format!("Error: {self}")
} else {
buff
}
}
}
#[derive(Debug, ThisError, Diagnostic)]
#[error("Could not perform IO on file `{file_path}`: ({err})")]
pub struct PersistError {
pub(crate) file_path: PathBuf,
pub(crate) err: std::io::Error,
}
impl PersistError {
pub fn wrap(path: impl Into<PathBuf>) -> impl FnOnce(std::io::Error) -> PersistError {
|err| PersistError {
file_path: path.into(),
err,
}
}
}