use std::fmt::Debug;
use mz_proto::TryFromProtoError;
use mz_repr::Timestamp;
use mz_sql::catalog::CatalogError as SqlCatalogError;
use mz_storage_types::controller::StorageError;
#[derive(Debug, thiserror::Error)]
pub enum CatalogError {
#[error(transparent)]
Catalog(#[from] SqlCatalogError),
#[error(transparent)]
Durable(#[from] DurableCatalogError),
}
impl From<TryFromProtoError> for CatalogError {
fn from(e: TryFromProtoError) -> Self {
Self::Durable(e.into())
}
}
#[derive(Debug, thiserror::Error)]
pub enum DurableCatalogError {
#[error("{0}")]
Fence(String),
#[error(
"incompatible Catalog version {found_version}, minimum: {min_catalog_version}, current: {catalog_version}"
)]
IncompatibleDataVersion {
found_version: u64,
min_catalog_version: u64,
catalog_version: u64,
},
#[error("incompatible persist version {found_version}, current: {catalog_version}, make sure to upgrade the catalog one version at a time")]
IncompatiblePersistVersion {
found_version: semver::Version,
catalog_version: semver::Version,
},
#[error("uninitialized")]
Uninitialized,
#[error("{0}")]
NotWritable(String),
#[error("proto: {0}")]
Proto(TryFromProtoError),
#[error("duplicate key")]
DuplicateKey,
#[error("uniqueness violation")]
UniquenessViolation,
#[error(transparent)]
Storage(StorageError<Timestamp>),
}
impl DurableCatalogError {
pub fn should_halt(&self) -> bool {
match self {
DurableCatalogError::Fence(_)
| DurableCatalogError::IncompatibleDataVersion { .. }
| DurableCatalogError::IncompatiblePersistVersion { .. }
| DurableCatalogError::Proto(_) => true,
DurableCatalogError::Uninitialized
| DurableCatalogError::NotWritable(_)
| DurableCatalogError::DuplicateKey
| DurableCatalogError::UniquenessViolation
| DurableCatalogError::Storage(_) => false,
}
}
pub fn is_unrecoverable(&self) -> bool {
match self {
DurableCatalogError::Fence(_) | DurableCatalogError::Uninitialized => true,
_ => false,
}
}
pub fn can_recover_with_write_mode(&self) -> bool {
match self {
DurableCatalogError::NotWritable(_) => true,
_ => false,
}
}
}
impl From<StorageError<Timestamp>> for DurableCatalogError {
fn from(e: StorageError<Timestamp>) -> Self {
DurableCatalogError::Storage(e)
}
}
impl From<TryFromProtoError> for DurableCatalogError {
fn from(e: TryFromProtoError) -> Self {
DurableCatalogError::Proto(e)
}
}