1#[derive(Debug, thiserror::Error)]
3pub enum ErdifyError {
4 #[error("invalid url error: {0}")]
5 InvalidUrl(String),
6
7 #[error("database connection error: {0}")]
8 DatabaseConnection(String),
9
10 #[error("query error: {0}")]
11 QueryError(String),
12
13 #[error("file write error: {0}")]
14 IoError(#[from] std::io::Error),
15
16 #[error("no table found for the specified filters")]
17 NoTablesFound,
18
19 #[error("connection timeout (10s exceeded)")]
20 ConnectionTimeout,
21
22 #[error("no valid schema found")]
23 NoValidSchemas,
24}
25
26impl From<tokio_postgres::error::Error> for ErdifyError {
27 fn from(err: tokio_postgres::error::Error) -> Self {
28 Self::DatabaseConnection(err.to_string())
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn invalid_url_displays_the_reason() {
38 let err = ErdifyError::InvalidUrl("no host in the url".to_string());
39 assert_eq!(err.to_string(), "invalid url error: no host in the url");
40 }
41
42 #[test]
43 fn database_connection_displays_the_reason() {
44 let err = ErdifyError::DatabaseConnection("connection refused".to_string());
45 assert_eq!(
46 err.to_string(),
47 "database connection error: connection refused"
48 );
49 }
50
51 #[test]
52 fn query_error_displays_the_reason() {
53 let err = ErdifyError::QueryError("unknown relation".to_string());
54 assert_eq!(err.to_string(), "query error: unknown relation");
55 }
56
57 #[test]
58 fn no_tables_found_has_a_fixed_message() {
59 let err = ErdifyError::NoTablesFound;
60 assert_eq!(err.to_string(), "no table found for the specified filters");
61 }
62
63 #[test]
64 fn connection_timeout_has_a_fixed_message() {
65 let err = ErdifyError::ConnectionTimeout;
66 assert_eq!(err.to_string(), "connection timeout (10s exceeded)");
67 }
68
69 #[test]
70 fn no_valid_schemas_has_a_fixed_message() {
71 let err = ErdifyError::NoValidSchemas;
72 assert_eq!(err.to_string(), "no valid schema found");
73 }
74
75 #[test]
76 fn io_error_is_wrapped_and_displayed() {
77 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
78 let err: ErdifyError = io_err.into();
79 assert_eq!(err.to_string(), "file write error: file not found");
80 }
81}