runlimit_postgres/
errors.rs1use runlimit_core::{BatchError, ConsumptionStatus};
2use thiserror::Error;
3
4#[derive(Debug, Error)]
6#[non_exhaustive]
7pub enum CheckError {
8 #[error(transparent)]
10 InvalidBatch(#[from] BatchError),
11
12 #[error("PostgreSQL rate-limit check failed before commit; quota was not consumed")]
14 DefinitelyNotConsumed(#[source] sqlx::Error),
15
16 #[error("PostgreSQL rate-limit commit outcome is unknown; quota may have been consumed")]
18 CommitOutcomeUnknown(#[source] sqlx::Error),
19
20 #[error("PostgreSQL rate-limit check timed out while {operation}; quota was not consumed")]
23 TimedOutBeforeCommit {
24 operation: &'static str,
26 },
27
28 #[error("PostgreSQL rate-limit commit timed out; quota may have been consumed")]
30 CommitTimedOut,
31
32 #[error("PostgreSQL rate-limit storage invariant failed: {0}")]
34 StorageInvariant(&'static str),
35
36 #[error(
38 "PostgreSQL rate-limit single-check response invariant failed after rollback; quota was not consumed"
39 )]
40 ResponseInvariant,
41
42 #[error(
44 "PostgreSQL rate-limit single-check response invariant failed after commit; quota was consumed"
45 )]
46 CommittedResponseInvariant,
47}
48
49impl CheckError {
50 pub const fn may_have_consumed_quota(&self) -> bool {
52 matches!(
53 self,
54 Self::CommitOutcomeUnknown(_) | Self::CommitTimedOut | Self::CommittedResponseInvariant
55 )
56 }
57}
58
59#[derive(Debug, Error)]
61#[non_exhaustive]
62pub enum MaintenanceError {
63 #[error("PostgreSQL expired-window cleanup failed before commit; no rows were removed")]
66 Database(#[source] sqlx::Error),
67 #[error("PostgreSQL expired-window cleanup timed out while {operation}; no rows were removed")]
70 TimedOutBeforeCommit {
71 operation: &'static str,
73 },
74 #[error(
76 "PostgreSQL expired-window cleanup commit outcome is unknown; rows may have been removed"
77 )]
78 CommitOutcomeUnknown(#[source] sqlx::Error),
79 #[error("PostgreSQL expired-window cleanup commit timed out; rows may have been removed")]
81 CommitTimedOut,
82}
83
84impl MaintenanceError {
85 pub const fn may_have_removed_rows(&self) -> bool {
87 matches!(self, Self::CommitOutcomeUnknown(_) | Self::CommitTimedOut)
88 }
89}
90
91pub(crate) const fn check_error_consumption(error: &CheckError) -> ConsumptionStatus {
92 match error {
93 CheckError::CommitOutcomeUnknown(_) | CheckError::CommitTimedOut => {
94 ConsumptionStatus::PossiblyConsumed
95 }
96 CheckError::CommittedResponseInvariant => ConsumptionStatus::Consumed,
97 _ => ConsumptionStatus::NotConsumed,
98 }
99}