Skip to main content

coven_database/
open_error.rs

1//! Why opening the database failed: its connections, lock, settings, coven's
2//! own schema or the app's migrations (§4, E1, E13).
3
4use crate::DbError;
5use coven_foundation::files::{SettingsError, StoreLockError};
6use coven_foundation::task::{Bug, CarriesBug};
7
8/// Opening the database failed: its writer or read-only connections, its
9/// lock, its settings or its migrations (§4, E1, E13).
10#[derive(Debug, thiserror::Error)]
11pub enum OpenError {
12    /// The database failed.
13    #[error(transparent)]
14    Database(#[from] DbError),
15    /// An app migration failed or cannot run on this schema.
16    #[error(transparent)]
17    Migration(#[from] MigrationError),
18    /// Coven's internal schema is unsupported or its tables cannot be created.
19    #[error(transparent)]
20    CovenSchema(#[from] CovenSchemaError),
21    /// The directory's settings could not be read.
22    #[error(transparent)]
23    Settings(SettingsError),
24    /// The store's lock could not be taken.
25    #[error(transparent)]
26    Lock(StoreLockError),
27    /// A required builder choice was not supplied (E1).
28    #[error("opening requires {field}")]
29    MissingConfiguration {
30        /// The missing choice.
31        field: &'static str,
32    },
33    /// Taking the store's lock panicked: a bug, which the caller reports
34    /// (E15). Opening's database work that panicked is `DbError::Bug`.
35    #[error(transparent)]
36    Bug(#[from] coven_foundation::task::Bug),
37}
38
39impl CarriesBug for OpenError {
40    fn bug(&self) -> Option<&Bug> {
41        match self {
42            Self::Bug(bug) => Some(bug),
43            Self::Database(error) => error.bug(),
44            Self::Lock(error) => error.bug(),
45            Self::Settings(error) => error.bug(),
46            Self::Migration(MigrationError::Failed { source, .. })
47            | Self::CovenSchema(CovenSchemaError::Failed { source }) => source.bug(),
48            Self::Migration(
49                MigrationError::NotContiguous { .. } | MigrationError::SchemaTooNew { .. },
50            )
51            | Self::CovenSchema(
52                CovenSchemaError::ReadOnly | CovenSchemaError::SchemaTooNew { .. },
53            )
54            | Self::MissingConfiguration { .. } => None,
55        }
56    }
57}
58
59impl From<SettingsError> for OpenError {
60    fn from(error: SettingsError) -> Self {
61        match error {
62            SettingsError::Bug(bug) => Self::Bug(bug),
63            error => Self::Settings(error),
64        }
65    }
66}
67
68impl From<StoreLockError> for OpenError {
69    fn from(error: StoreLockError) -> Self {
70        match error {
71            StoreLockError::Bug(bug) => Self::Bug(bug),
72            error => Self::Lock(error),
73        }
74    }
75}
76
77impl From<sqlive::rusqlite::Error> for OpenError {
78    fn from(error: sqlive::rusqlite::Error) -> Self {
79        Self::Database(error.into())
80    }
81}
82
83/// Coven's local tables cannot be used at this version (Appendix C.1, E1).
84#[derive(Debug, thiserror::Error)]
85pub enum CovenSchemaError {
86    /// The database holds no coven tables, and the read-only connection cannot create them.
87    #[error("read-only open cannot create coven's schema")]
88    ReadOnly,
89    /// The internal schema requires a newer coven.
90    #[error("coven schema version {current} is newer than supported version {supported}")]
91    SchemaTooNew {
92        /// The database's internal schema version.
93        current: u32,
94        /// The internal schema version this coven supports.
95        supported: u32,
96    },
97    /// Creating the tables failed and its transaction rolled back.
98    #[error("creating coven's schema failed: {source}")]
99    Failed {
100        /// The failure, including any rollback error.
101        #[source]
102        source: Box<DbError>,
103    },
104}
105
106/// The application migration run could not complete.
107#[derive(Debug, thiserror::Error)]
108pub enum MigrationError {
109    /// The list is not numbered from one without gaps.
110    #[error("migration at {position} has version {found}, expected {expected}")]
111    NotContiguous {
112        /// Zero-based position in the list.
113        position: usize,
114        /// Declared version.
115        found: u32,
116        /// Required version.
117        expected: u32,
118    },
119    /// The database requires a newer app.
120    #[error("schema version {current} is newer than supported version {supported}")]
121    SchemaTooNew {
122        /// The database version.
123        current: u32,
124        /// The last supplied migration.
125        supported: u32,
126    },
127    /// The migration or final validation failed; the whole run rolled back.
128    #[error("migration {version} ({name}) failed: {source}")]
129    Failed {
130        /// Migration being run, or the final migration for validation errors.
131        version: u32,
132        /// Its name.
133        name: &'static str,
134        /// The original failure.
135        #[source]
136        source: Box<DbError>,
137    },
138}