Skip to main content

gaman_core/states/
errors.rs

1use thiserror::Error;
2
3#[derive(Debug, Error, PartialEq)]
4pub enum SchemaValidationError {
5    #[error("{0}")]
6    Invalid(String),
7}
8
9impl From<String> for SchemaValidationError {
10    fn from(value: String) -> Self {
11        Self::Invalid(value)
12    }
13}
14
15impl From<&str> for SchemaValidationError {
16    fn from(value: &str) -> Self {
17        Self::Invalid(value.to_string())
18    }
19}
20
21#[derive(Debug, Error, PartialEq)]
22pub enum ReplayError {
23    #[error("migration '{0}' not found in graph")]
24    MigrationNotFound(String),
25    #[error("table '{0}' already exists")]
26    TableAlreadyExists(String),
27    #[error("table '{0}' not found")]
28    TableNotFound(String),
29    #[error("table '{new}' already exists, cannot rename '{old}' to it")]
30    RenameTargetExists { old: String, new: String },
31    #[error("column '{column}' already exists on table '{table}'")]
32    ColumnAlreadyExists { table: String, column: String },
33    #[error("column '{column}' not found on table '{table}'")]
34    ColumnNotFound { table: String, column: String },
35    #[error("foreign key '{fk}' already exists on table '{table}'")]
36    ForeignKeyAlreadyExists { table: String, fk: String },
37    #[error("foreign key '{fk}' not found on table '{table}'")]
38    ForeignKeyNotFound { table: String, fk: String },
39    #[error("index '{index}' already exists on table '{table}'")]
40    IndexAlreadyExists { table: String, index: String },
41    #[error("index '{index}' not found on table '{table}'")]
42    IndexNotFound { table: String, index: String },
43    #[error("constraint '{constraint}' already exists on table '{table}'")]
44    ConstraintAlreadyExists { table: String, constraint: String },
45    #[error("constraint '{constraint}' not found on table '{table}'")]
46    ConstraintNotFound { table: String, constraint: String },
47    #[error("table '{0}' has multiple primary key declarations")]
48    MultiplePrimaryKeys(String),
49    #[error(
50        "primary key changes on existing table '{0}' are not generated automatically; use an explicit SQL statement migration"
51    )]
52    PrimaryKeyMutation(String),
53    #[error("function '{0}' already exists")]
54    FunctionAlreadyExists(String),
55    #[error("function '{0}' not found")]
56    FunctionNotFound(String),
57    #[error("trigger '{trigger}' already exists on table '{table}'")]
58    TriggerAlreadyExists { table: String, trigger: String },
59    #[error("trigger '{trigger}' not found on table '{table}'")]
60    TriggerNotFound { table: String, trigger: String },
61    #[error("view '{0}' already exists")]
62    ViewAlreadyExists(String),
63    #[error("view '{0}' not found")]
64    ViewNotFound(String),
65    #[error("extension '{0}' already exists")]
66    ExtensionAlreadyExists(String),
67    #[error("extension '{0}' not found")]
68    ExtensionNotFound(String),
69    #[error("enum '{0}' already exists")]
70    EnumAlreadyExists(String),
71    #[error("enum '{0}' not found")]
72    EnumNotFound(String),
73    #[error("in migration '{migration}' (operation {op_num})")]
74    WithContext {
75        migration: String,
76        op_num: usize,
77        #[source]
78        inner: Box<ReplayError>,
79    },
80}
81
82#[derive(Debug, Error)]
83pub enum SchemaLoadError {
84    #[error("cannot read '{0}': {1}")]
85    Io(String, #[source] std::io::Error),
86    #[error("in schema '{path}': {source}")]
87    Path {
88        path: String,
89        #[source]
90        source: Box<SchemaLoadError>,
91    },
92    #[error("invalid YAML: {0}")]
93    Yaml(#[from] serde_yaml::Error),
94    #[error("invalid JSON: {0}")]
95    Json(#[from] serde_json::Error),
96    #[error("table '{table}' defined in both '{a}' and '{b}'")]
97    Merge { table: String, a: String, b: String },
98    #[error("duplicate table '{0}' when merging schemas")]
99    DuplicateTable(String),
100    #[error("schema validation failed: {0}")]
101    Validation(#[from] SchemaValidationError),
102    #[error(transparent)]
103    Sql(Box<crate::parsers::ParseError>),
104}
105
106impl From<crate::parsers::ParseError> for SchemaLoadError {
107    fn from(error: crate::parsers::ParseError) -> Self {
108        Self::Sql(Box::new(error))
109    }
110}