cornucopia/
error.rs

1use miette::{Diagnostic, GraphicalReportHandler, GraphicalTheme};
2use thiserror::Error as ThisError;
3
4/// Enumeration of all the errors reported by Cornucopia.
5#[derive(Debug, ThisError, Diagnostic)]
6#[error(transparent)]
7#[diagnostic(transparent)]
8pub enum Error {
9    /// An error while trying to connect to a database.
10    Connection(#[from] crate::conn::error::Error),
11    /// An error while trying to read PostgreSQL query files.
12    ReadQueries(#[from] crate::read_queries::error::Error),
13    /// An error while trying to parse PostgreSQL query files.
14    ParseQueries(#[from] crate::parser::error::Error),
15    /// An error while trying to validate PostgreSQL query files.
16    ValidateQueries(#[from] Box<crate::validation::error::Error>),
17    /// An error while manipulating a container managed by Cornucopia.
18    Container(#[from] crate::container::error::Error),
19    /// An error while trying to prepare PostgreSQL queries.
20    PrepareQueries(#[from] crate::prepare_queries::error::Error),
21    /// An error while reading PostgreSQL schema files.
22    LoadSchema(#[from] crate::load_schema::error::Error),
23    /// An error while trying to write the generated code to its destination file.
24    WriteCodeGenFile(#[from] WriteOutputError),
25}
26
27impl Error {
28    #[must_use]
29    pub fn report(self) -> String {
30        let mut buff = String::new();
31        GraphicalReportHandler::new()
32            .with_theme(GraphicalTheme::unicode_nocolor())
33            .render_report(&mut buff, &self)
34            .unwrap();
35        buff
36    }
37}
38
39#[derive(Debug, ThisError, Diagnostic)]
40#[error("Could not write your queries to destination file `{file_path}`: ({err})")]
41pub struct WriteOutputError {
42    pub(crate) file_path: String,
43    pub(crate) err: std::io::Error,
44}