Skip to main content

coven_database/
reported_engine_errors.rs

1//! How coven reports the engine's errors: coven turns each engine error into
2//! the `DbError` an app sees.
3
4use crate::store_failure::StoreFailure;
5use crate::{DbError, OpenError};
6
7/// Coven turns an engine error into the `DbError` an app sees. An error
8/// coven raised inside a SQL function, or a refusal of coven's policy, comes
9/// back as coven raised it.
10impl From<sqlive::Error<StoreFailure, DbError>> for DbError {
11    fn from(error: sqlive::Error<StoreFailure, DbError>) -> Self {
12        match error {
13            sqlive::Error::Sqlite(error) => Self::from(error),
14            sqlive::Error::DamagedDatabase => Self::DamagedDatabase,
15            sqlive::Error::Refused(refusal) => refusal,
16            sqlive::Error::StatementForbidden { operation } => {
17                Self::StatementForbidden { operation }
18            }
19            sqlive::Error::TransactionEnded => Self::TransactionEnded,
20            sqlive::Error::Closed => Self::StoreClosed,
21            sqlive::Error::Bug(bug) => Self::Bug(bug),
22            sqlive::Error::HandleFailed(bug) => Self::StoreFailed(bug),
23            sqlive::Error::Host(failure) => failure.error(),
24            sqlive::Error::Invariant { what } => Self::Invariant { what },
25            sqlive::Error::WalUnavailable { mode } => Self::WalUnavailable { mode },
26            sqlive::Error::SqliteSettingOutOfRange { setting, maximum } => {
27                Self::SqliteSettingOutOfRange { setting, maximum }
28            }
29            sqlive::Error::UpkeepThread(error) => Self::UpkeepThread(error),
30            sqlive::Error::Path { path, source } => {
31                Self::Disk(coven_foundation::files::FileError::Io {
32                    operation: "open database",
33                    path,
34                    source: Box::new(source),
35                })
36            }
37            sqlive::Error::Rollback { operation, rollback } => {
38                Self::Rollback { operation: Box::new(Self::from(*operation)), rollback }
39            }
40        }
41    }
42}
43
44impl From<sqlive::Error<StoreFailure, DbError>> for OpenError {
45    fn from(error: sqlive::Error<StoreFailure, DbError>) -> Self {
46        Self::Database(error.into())
47    }
48}
49
50/// Return the `DbError` that an error raised inside a SQL function or a
51/// column conversion carries, whether coven or the engine raised it, and
52/// return any other error unchanged.
53pub(crate) fn raised(
54    source: Box<dyn std::error::Error + Send + Sync>,
55) -> Result<DbError, Box<dyn std::error::Error + Send + Sync>> {
56    match source.downcast::<DbError>() {
57        Ok(error) => Ok(*error),
58        Err(source) => source
59            .downcast::<sqlive::Error<StoreFailure, DbError>>()
60            .map(|error| DbError::from(*error)),
61    }
62}
63
64#[cfg(test)]
65#[path = "reported_engine_errors_tests.rs"]
66mod tests;