use thiserror::Error;
use super::ProjectionId;
pub type ProjectionResult<T> = Result<T, ProjectionError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProjectionErrorKind {
Fatal,
Restartable,
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum ProjectionError {
#[error("projection cursor codec failed: {0}")]
CursorCodec(String),
#[error("projection provider manifest mismatch: expected {expected:?}, found {actual:?}")]
ProviderManifestMismatch {
expected: String,
actual: String,
},
#[error("invalid projection configuration for {setting}: {reason}")]
InvalidConfig {
setting: &'static str,
reason: String,
},
#[error("projection {projection_id} is already running in this process")]
AlreadyRunning {
projection_id: ProjectionId,
},
#[error(
"projection {projection_id} lost offset-store fencing: generation \
{attempted_generation} is not current generation {current_generation}"
)]
Fenced {
projection_id: ProjectionId,
attempted_generation: u64,
current_generation: u64,
},
#[error(
"projection {projection_id} cursor position conflict: expected {expected:?}, \
found {actual:?}"
)]
UnexpectedPosition {
projection_id: ProjectionId,
expected: Option<String>,
actual: Option<String>,
},
#[error("projection {projection_id} offset-store generation is exhausted")]
GenerationExhausted {
projection_id: ProjectionId,
},
#[error("projection source failed: {0}")]
Source(String),
#[error("projection handler failed: {0}")]
Handler(String),
#[error("projection offset store failed: {0}")]
OffsetStore(String),
#[error("projection runtime failed: {0}")]
Runtime(String),
#[error("projection testkit assertion timed out after {timeout_ms} ms")]
TestKitTimeout {
timeout_ms: u64,
},
}
impl ProjectionError {
#[must_use]
pub const fn kind(&self) -> ProjectionErrorKind {
match self {
Self::CursorCodec(_)
| Self::ProviderManifestMismatch { .. }
| Self::InvalidConfig { .. }
| Self::AlreadyRunning { .. }
| Self::Fenced { .. }
| Self::UnexpectedPosition { .. }
| Self::GenerationExhausted { .. }
| Self::Runtime(_)
| Self::TestKitTimeout { .. } => ProjectionErrorKind::Fatal,
Self::Source(_) | Self::Handler(_) | Self::OffsetStore(_) => {
ProjectionErrorKind::Restartable
}
}
}
#[must_use]
pub const fn is_fatal(&self) -> bool {
matches!(self.kind(), ProjectionErrorKind::Fatal)
}
#[must_use]
pub const fn is_restartable(&self) -> bool {
matches!(self.kind(), ProjectionErrorKind::Restartable)
}
pub(super) fn invalid_config(setting: &'static str, reason: impl Into<String>) -> Self {
Self::InvalidConfig {
setting,
reason: reason.into(),
}
}
}