Skip to main content

tmp_postgrust/
errors.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5/// UTF-8 captures of stdout and stderr for child processes used by the library.
6#[derive(Debug)]
7pub struct ProcessCapture {
8    /// Capture of stdout from the process
9    pub stdout: String,
10    /// Capture of stderr from the process
11    pub stderr: String,
12}
13
14/// Error type for possible postgresql errors.
15#[derive(Error, Debug)]
16#[non_exhaustive]
17pub enum TmpPostgrustError {
18    /// Catchall error for when a subprocess fails to run to completion
19    #[error("subprocess failed to execute")]
20    ExecSubprocessFailed {
21        /// Underlying I/O Error.
22        #[source]
23        source: std::io::Error,
24        /// Debug formatted string of Command that was attempted.
25        command: String,
26    },
27    /// Catchall error for when a subprocess fails to start
28    #[error("subprocess failed to spawn")]
29    SpawnSubprocessFailed(#[source] std::io::Error),
30    /// Error when `initdb` fails to execute.
31    #[error("initdb failed")]
32    InitDBFailed(ProcessCapture),
33    /// Error when a file to be copied is not found.
34    #[error("copying cached database failed, file not found")]
35    CopyCachedInitDBFailedFileNotFound(#[source] std::io::Error),
36    /// Error when the file type cannot be read when copying the cached db.
37    #[error("copying cached database failed, could not read file type")]
38    CopyCachedInitDBFailedCouldNotReadFileType(#[source] std::io::Error),
39    /// Error when the source directory filepath cannot be stripped.
40    #[error("copying cached database failed, could not strip path prefix")]
41    CopyCachedInitDBFailedCouldNotStripPathPrefix(#[source] std::path::StripPrefixError),
42    /// Error when a copy process cannot be joined.
43    #[cfg(feature = "tokio-process")]
44    #[error("copying cached database failed, failed to join cp process")]
45    CopyCachedInitDBFailedJoinError(#[source] tokio::task::JoinError),
46    /// Error when `createdb` fails to execute.
47    #[error("createdb failed")]
48    CreateDBFailed(ProcessCapture),
49    /// Error when `postgresql.conf` cannot be written.
50    #[error("failed to write postgresql.conf")]
51    CreateConfigFailed(#[source] std::io::Error),
52    /// Error when the PGDATA directory is empty.
53    #[error("failed to find temporary data directory")]
54    EmptyDataDirectory,
55    /// Error when the temporary unix socket directory cannot be created.
56    #[error("failed to create unix socket directory")]
57    CreateSocketDirFailed(#[source] std::io::Error),
58    /// Error when the cache directory cannot be created.
59    #[error("failed to create cache directory")]
60    CreateCacheDirFailed(#[source] std::io::Error),
61    /// Error when the data directory cannot be created.
62    #[error("failed to create cache directory")]
63    CreateDataDirFailed(#[source] std::io::Error),
64    /// Error when `cp` fails for the initialized database.
65    #[error("updating directory permission to non-root failed")]
66    UpdatingPermissionsFailed(ProcessCapture),
67    /// Error when running migrations failed.
68    #[error("failed to run database migrations")]
69    MigrationsFailed(#[source] Box<dyn std::error::Error + Send + Sync>),
70    /// Error when a required postgres binary cannot be found.
71    #[error("could not find postgres command `{command}`{}", .searched_dir.as_ref().map(|d| format!(" in {}", d.display())).unwrap_or_default())]
72    PostgresCommandNotFound {
73        /// Name of the binary that was not found.
74        command: String,
75        /// The explicit directory that was searched, if one was provided.
76        searched_dir: Option<PathBuf>,
77    },
78    /// Error when a process permit cannot be acquired for a new postgres subprocess.
79    #[cfg(feature = "tokio-process")]
80    #[error("acquiring process permit failed")]
81    AsyncProcessPermitAcquireError(#[source] tokio::sync::AcquireError),
82}
83
84/// Result type for `TmpPostgrustError`, used by functions in this crate.
85pub type TmpPostgrustResult<T> = Result<T, TmpPostgrustError>;