1use std::{io, process::Output};
2
3use crate::{cluster, runtime, util, version};
4
5#[derive(thiserror::Error, Debug)]
6pub enum ClusterError {
7 #[error("input/output error")]
8 IoError(#[from] io::Error),
9 #[error(
10 "cluster is PostgreSQL {0}, which is not supported; pgdo requires PostgreSQL {min} or later",
11 min = runtime::MINIMUM_VERSION
12 )]
13 UnsupportedVersion(version::PartialVersion),
14 #[error(
15 "PostgreSQL runtime {0} is not supported; pgdo requires PostgreSQL {min} or later",
16 min = runtime::MINIMUM_VERSION
17 )]
18 UnsupportedRuntime(version::Version),
19 #[error("PostgreSQL version not known")]
20 VersionError(#[from] version::VersionError),
21 #[error("PostgreSQL runtime not found for version {0}")]
22 RuntimeNotFound(version::PartialVersion),
23 #[error("PostgreSQL runtime not found")]
24 RuntimeDefaultNotFound,
25 #[error("runtime error")]
26 RuntimeError(#[from] runtime::RuntimeError),
27 #[error(transparent)]
28 ClientError(#[from] cluster::ClientError),
29 #[error(transparent)]
30 ConfigError(#[from] cluster::config::ConfigError),
31 #[error("data directory {0:?} is not a PostgreSQL cluster, and is not empty: {entries}", entries = .1.join(", "))]
32 DataDirNotEmpty(std::path::PathBuf, Vec<String>),
33 #[error(
34 "cluster is in use on another host or VM ({hostname}); last heartbeat {}",
35 super::owner::ago(*age)
36 )]
37 InUseElsewhere { hostname: String, age: std::time::Duration },
38 #[error(
39 "cluster was last used on another host or VM ({hostname}) {}, and may still be running there",
40 super::owner::ago(*age)
41 )]
42 StaleElsewhere { hostname: String, age: std::time::Duration },
43 #[error(
44 "cluster's postmaster.pid names PID {pid}, which is not a PostgreSQL server for this cluster"
45 )]
46 StalePidFile { pid: u32 },
47 #[error("could not tell whether the cluster is running in another namespace: could not connect to {path}")]
48 ServerStateUnknown {
49 path: std::path::PathBuf,
50 #[source]
51 source: io::Error,
52 },
53 #[error("interrupted while starting a session with the cluster")]
54 Interrupted,
55 #[error("could not lock cluster")]
56 LockError(#[from] nix::Error),
57 #[error("external command failed: {0:?}")]
58 CommandError(Output),
59 #[error(transparent)]
60 CurrentUserError(#[from] util::CurrentUserError),
61 #[error("URL error")]
62 UrlError(#[from] url::ParseError),
63}
64
65impl ClusterError {
66 pub fn help(&self) -> Option<String> {
72 match self {
73 Self::UnsupportedVersion(_) => Some(
74 "Upgrade the cluster with `pg_upgrade`, or use an older release of pgdo to work \
75 with it."
76 .into(),
77 ),
78 Self::DataDirNotEmpty(..) => Some(
79 "pgdo creates clusters only in empty directories (pgdo's own `pgdo.*` files \
80 excepted)."
81 .into(),
82 ),
83 Self::InUseElsewhere { .. } => Some(
84 "pgdo cannot share a cluster between kernels, e.g. between macOS and a Linux VM. \
85 Use the cluster from there, or stop using it there first."
86 .into(),
87 ),
88 Self::StaleElsewhere { hostname, .. } => Some(format!(
89 "If you are sure that nothing on {hostname} is using the cluster, take it over \
90 with `--force` (or `Takeover::IfStale`)."
91 )),
92 Self::StalePidFile { .. } => Some(
93 "Another process now has that PID. If you are sure that no PostgreSQL server is \
94 using this data directory, remove postmaster.pid."
95 .into(),
96 ),
97 Self::ServerStateUnknown { .. } => Some(
98 "If the socket's path is too long for this platform (103 bytes on macOS, 107 on \
99 Linux), use a data directory with a shorter path."
100 .into(),
101 ),
102 Self::ClientError(err) => err.help(),
103 Self::IoError(_)
106 | Self::UnsupportedRuntime(_)
107 | Self::VersionError(_)
108 | Self::RuntimeNotFound(_)
109 | Self::RuntimeDefaultNotFound
110 | Self::RuntimeError(_)
111 | Self::ConfigError(_)
112 | Self::Interrupted
113 | Self::LockError(_)
114 | Self::CommandError(_)
115 | Self::CurrentUserError(_)
116 | Self::UrlError(_) => None,
117 }
118 }
119}