use runlimit_core::{BatchError, ConsumptionStatus};
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CheckError {
#[error(transparent)]
InvalidBatch(#[from] BatchError),
#[error("PostgreSQL rate-limit check failed before commit; quota was not consumed")]
DefinitelyNotConsumed(#[source] sqlx::Error),
#[error("PostgreSQL rate-limit commit outcome is unknown; quota may have been consumed")]
CommitOutcomeUnknown(#[source] sqlx::Error),
#[error("PostgreSQL rate-limit check timed out while {operation}; quota was not consumed")]
TimedOutBeforeCommit {
operation: &'static str,
},
#[error("PostgreSQL rate-limit commit timed out; quota may have been consumed")]
CommitTimedOut,
#[error("PostgreSQL rate-limit storage invariant failed: {0}")]
StorageInvariant(&'static str),
#[error(
"PostgreSQL rate-limit single-check response invariant failed after rollback; quota was not consumed"
)]
ResponseInvariant,
#[error(
"PostgreSQL rate-limit single-check response invariant failed after commit; quota was consumed"
)]
CommittedResponseInvariant,
}
impl CheckError {
pub const fn may_have_consumed_quota(&self) -> bool {
matches!(
self,
Self::CommitOutcomeUnknown(_) | Self::CommitTimedOut | Self::CommittedResponseInvariant
)
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum MaintenanceError {
#[error("PostgreSQL expired-window cleanup failed before commit; no rows were removed")]
Database(#[source] sqlx::Error),
#[error("PostgreSQL expired-window cleanup timed out while {operation}; no rows were removed")]
TimedOutBeforeCommit {
operation: &'static str,
},
#[error(
"PostgreSQL expired-window cleanup commit outcome is unknown; rows may have been removed"
)]
CommitOutcomeUnknown(#[source] sqlx::Error),
#[error("PostgreSQL expired-window cleanup commit timed out; rows may have been removed")]
CommitTimedOut,
}
impl MaintenanceError {
pub const fn may_have_removed_rows(&self) -> bool {
matches!(self, Self::CommitOutcomeUnknown(_) | Self::CommitTimedOut)
}
}
pub(crate) const fn check_error_consumption(error: &CheckError) -> ConsumptionStatus {
match error {
CheckError::CommitOutcomeUnknown(_) | CheckError::CommitTimedOut => {
ConsumptionStatus::PossiblyConsumed
}
CheckError::CommittedResponseInvariant => ConsumptionStatus::Consumed,
_ => ConsumptionStatus::NotConsumed,
}
}