safety_postgres/access/
errors.rs

1use std::fmt;
2use std::error::Error;
3use std::fmt::{Debug, Formatter};
4
5
6/// A trait for generating custom error values.
7///
8/// This trait defines a method `generate_error` that takes
9/// a string message and returns an error of generic type `E`.
10/// Implementations of this trait can be used to generate
11/// custom error values for error handling.
12pub(super) trait ErrorGenerator<E> {
13    fn generate_error(&self, msg: String) -> E;
14}
15
16/// Represents an error that occurs during joining of tables.
17#[derive(Debug, PartialEq)]
18pub enum JoinTableError {
19    InputInconsistentError(String),
20    InputInvalidError(String),
21}
22
23impl fmt::Display for JoinTableError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::InputInconsistentError(e) => write!(f, "Error occurred during parsing the collection input in preparing join table process due to {}", e),
27            Self::InputInvalidError(e) => write!(f, "Error occurred during validating the input data in preparing join table process due to {}", e),
28        }
29    }
30}
31
32impl Error for JoinTableError {}
33
34/// The `JoinTableErrorGenerator` struct is used internally in a specific module
35/// to generate join table errors.
36pub(super) struct JoinTableErrorGenerator;
37
38impl ErrorGenerator<JoinTableError> for JoinTableErrorGenerator {
39    fn generate_error(&self, msg: String) -> JoinTableError {
40        JoinTableError::InputInvalidError(msg)
41    }
42}
43
44/// Represents an error that occurs when there is an invalid condition.
45#[derive(Debug, PartialEq)]
46pub enum ConditionError {
47    InputInvalidError(String),
48}
49
50impl fmt::Display for ConditionError {
51    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
52        match self {
53            Self::InputInvalidError(e) => write!(f, "Error occurred during validating the input data in condition prepare process due to {}", e),
54        }
55    }
56}
57
58impl Error for ConditionError {}
59
60/// The `ConditionErrorGenerator` struct is used internally in a specific module
61/// to generate condition errors.
62pub(super) struct ConditionErrorGenerator;
63
64impl ErrorGenerator<ConditionError> for ConditionErrorGenerator {
65    fn generate_error(&self, msg: String) -> ConditionError {
66        ConditionError::InputInvalidError(msg)
67    }
68}
69
70/// Represents an error that occurs during handling of query columns.
71#[derive(Debug, PartialEq)]
72pub enum QueryColumnError {
73    InputInvalidError(String),
74    InputInconsistentError(String),
75}
76
77impl fmt::Display for QueryColumnError {
78    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
79        match self {
80            Self::InputInvalidError(e) => write!(f, "Error occurred during validating the input data in query column process due to {}", e),
81            Self::InputInconsistentError(e) => write!(f, "Error occurred during query text build process in query column process due to {}", e),
82        }
83    }
84}
85
86impl Error for QueryColumnError {}
87
88/// The `QueryColumnErrorGenerator` struct is used internally in a specific module
89/// to generate query column errors.
90pub(super) struct QueryColumnErrorGenerator;
91
92impl ErrorGenerator<QueryColumnError> for QueryColumnErrorGenerator {
93    fn generate_error(&self, msg: String) -> QueryColumnError {
94        QueryColumnError::InputInvalidError(msg)
95    }
96}
97
98/// Represents an error that occurs when creating an update set.
99#[derive(Debug, PartialEq)]
100pub enum UpdateSetError {
101    InputInvalidError(String),
102}
103
104impl fmt::Display for UpdateSetError {
105    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
106        match self {
107            Self::InputInvalidError(e) => write!(f, "Error occurred during validating the input data in update values process due to {}", e),
108        }
109    }
110}
111
112impl Error for UpdateSetError {}
113
114/// The `UpdateSetErrorGenerator` struct is used internally in a specific module
115/// to generate update set errors.
116pub(super) struct UpdateSetErrorGenerator;
117impl ErrorGenerator<UpdateSetError> for UpdateSetErrorGenerator {
118    fn generate_error(&self, msg: String) -> UpdateSetError {
119        UpdateSetError::InputInvalidError(msg)
120    }
121}
122
123/// Represents an error that occurs during the insertion of a value.
124#[derive(Debug, PartialEq)]
125pub enum InsertValueError {
126    InputInvalidError(String),
127    InputInconsistentError(String),
128}
129
130impl fmt::Display for InsertValueError {
131    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
132        match self {
133            Self::InputInvalidError(e) => write!(f, "Error occurred during validating the input data in insert values process due to {}", e),
134            Self::InputInconsistentError(e) => write!(f, "Error occurred during check the input data in insert values process due to {}", e),
135        }
136    }
137}
138
139impl Error for InsertValueError {}
140
141/// The `InsertValueErrorGenerator` struct is used internally in a specific module
142/// to generate insert value errors.
143pub(super) struct InsertValueErrorGenerator;
144
145impl ErrorGenerator<InsertValueError> for InsertValueErrorGenerator {
146    fn generate_error(&self, msg: String) -> InsertValueError {
147        InsertValueError::InputInvalidError(msg)
148    }
149}
150
151/// Represents an error that can occur in the PostgreSQL interface.
152#[derive(Debug, PartialEq)]
153pub enum PostgresBaseError {
154    InputInvalidError(String),
155    ConfigNotDefinedError(String),
156    UnsafeExecutionError(String),
157    UnexpectedError(String),
158    ConnectionNotFoundError(String),
159    SQLExecutionError(String),
160    TokioPostgresError(String),
161}
162
163impl fmt::Display for PostgresBaseError {
164    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
165        match self {
166            Self::InputInvalidError(e) => write!(f, "Error occurred during validating the input data in postgres execution process due to {}", e),
167            Self::ConfigNotDefinedError(e) => write!(f, "Config doesn't exist in your environment arguments. {}", e),
168            Self::UnsafeExecutionError(e) => write!(f, "Unsafe SQL execution is detected from {}.", e),
169            Self::UnexpectedError(e) => write!(f, "Critical error occurred due to {}", e),
170            Self::ConnectionNotFoundError(e) => write!(f, "SQL execution need connection but it can't be found. {}", e),
171            Self::SQLExecutionError(e) => write!(f, "SQL execution failed due to {}", e),
172            Self::TokioPostgresError(e) => write!(f, "Get error from tokio-postgres crate: {}", e),
173        }
174    }
175}
176
177impl Error for PostgresBaseError {}