sql_from_models/
error.rs

1use crate::prelude::*;
2
3use sql_from_models_parser::parser::ParserError;
4
5use std::sync::Arc;
6use thiserror::Error;
7
8macro_rules! error {
9    ($($args:expr),+) => {
10        Error::Message(format!($($args),*))
11    };
12}
13
14#[derive(Error, Debug, Clone)]
15pub enum Error {
16    #[error("syntax error: {0}")]
17    Syntax(#[from] ParserError),
18    #[error("syntax error: {0}.\n       found at file \"{1}\".")]
19    SyntaxAtFile(ParserError, path::PathBuf),
20    #[error("{0}")]
21    Message(String),
22    #[error("could not read or create migration file. {0}")]
23    IO(#[from] Arc<io::Error>),
24    #[error("dependency cycle detected invlonving the tables: {0:?}. help: consider removing redundant foreign key constraints. ")]
25    Cycle(Vec<String>),
26}
27
28impl Error {
29    pub(crate) fn kind(&self) -> &'static str {
30        match self {
31            Self::Cycle(_) => "CycleError",
32            Self::Message(_) => "error",
33            Self::IO(_) => "IOError",
34            Self::Syntax(_) => "SyntaxError",
35            Self::SyntaxAtFile(_, _) => "SyntaxAtFile",
36        }
37    }
38
39    pub(crate) fn as_json(&self) -> String {
40        let err_msg = format!("{}", self);
41        let kind = self.kind();
42
43        format!(
44            r#"{{"kind":{kind:?},"message":{message:?}}}"#,
45            kind = kind,
46            message = err_msg
47        )
48    }
49}
50
51impl From<io::Error> for Error {
52    fn from(err: io::Error) -> Error {
53        Error::IO(Arc::new(err))
54    }
55}