tmp_postgrust/
errors.rs

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