postgresfixture/cluster/
error.rs

1use std::path::PathBuf;
2use std::process::Output;
3use std::{error, fmt, io};
4
5use crate::runtime;
6use crate::version;
7
8#[derive(Debug)]
9pub enum ClusterError {
10    PathEncodingError, // Path is not UTF-8.
11    IoError(io::Error),
12    UnixError(nix::Error),
13    UnsupportedVersion(version::Version),
14    UnknownVersion(version::VersionError),
15    RuntimeNotFound(version::PartialVersion),
16    RuntimeDefaultNotFound,
17    DataDirectoryNotFound(PathBuf),
18    DatabaseError(postgres::error::Error),
19    InUse, // Cluster is already in use; cannot lock exclusively.
20    Other(Output),
21}
22
23impl fmt::Display for ClusterError {
24    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
25        use ClusterError::*;
26        match *self {
27            PathEncodingError => write!(fmt, "path is not UTF-8"),
28            IoError(ref e) => write!(fmt, "input/output error: {e}"),
29            UnixError(ref e) => write!(fmt, "UNIX error: {e}"),
30            UnsupportedVersion(ref e) => write!(fmt, "PostgreSQL version not supported: {e}"),
31            UnknownVersion(ref e) => write!(fmt, "PostgreSQL version not known: {e}"),
32            RuntimeNotFound(ref v) => write!(fmt, "PostgreSQL runtime not found for version {v}"),
33            RuntimeDefaultNotFound => write!(fmt, "PostgreSQL runtime not found"),
34            DataDirectoryNotFound(ref p) => write!(fmt, "data directory not found in {p:?}"),
35            DatabaseError(ref e) => write!(fmt, "database error: {e}"),
36            InUse => write!(fmt, "cluster in use; cannot lock exclusively"),
37            Other(ref e) => write!(fmt, "external command failed: {e:?}"),
38        }
39    }
40}
41
42impl error::Error for ClusterError {
43    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
44        match *self {
45            ClusterError::PathEncodingError => None,
46            ClusterError::IoError(ref error) => Some(error),
47            ClusterError::UnixError(ref error) => Some(error),
48            ClusterError::UnsupportedVersion(_) => None,
49            ClusterError::UnknownVersion(ref error) => Some(error),
50            ClusterError::RuntimeNotFound(_) => None,
51            ClusterError::RuntimeDefaultNotFound => None,
52            ClusterError::DataDirectoryNotFound(_) => None,
53            ClusterError::DatabaseError(ref error) => Some(error),
54            ClusterError::InUse => None,
55            ClusterError::Other(_) => None,
56        }
57    }
58}
59
60impl From<io::Error> for ClusterError {
61    fn from(error: io::Error) -> ClusterError {
62        ClusterError::IoError(error)
63    }
64}
65
66impl From<nix::Error> for ClusterError {
67    fn from(error: nix::Error) -> ClusterError {
68        ClusterError::UnixError(error)
69    }
70}
71
72impl From<version::VersionError> for ClusterError {
73    fn from(error: version::VersionError) -> ClusterError {
74        ClusterError::UnknownVersion(error)
75    }
76}
77
78impl From<postgres::error::Error> for ClusterError {
79    fn from(error: postgres::error::Error) -> ClusterError {
80        ClusterError::DatabaseError(error)
81    }
82}
83
84impl From<runtime::RuntimeError> for ClusterError {
85    fn from(error: runtime::RuntimeError) -> ClusterError {
86        match error {
87            runtime::RuntimeError::IoError(error) => ClusterError::IoError(error),
88            runtime::RuntimeError::VersionError(error) => ClusterError::UnknownVersion(error),
89        }
90    }
91}