Skip to main content

rings_measure/
error.rs

1use thiserror::Error;
2
3use crate::Metric;
4use crate::UnixTime;
5
6/// Invalid measurement policy configuration.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
8pub enum PolicyError {
9    /// A reliability window must contain at least one second.
10    #[error("reliability window must be non-zero")]
11    ZeroReliabilityWindow,
12    /// Credit retention must contain at least one second.
13    #[error("credit retention must be non-zero")]
14    ZeroCreditRetention,
15}
16
17/// Failure of a pure measurement state transition or snapshot validation.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
19pub enum MeasureError {
20    /// A checked counter update exceeded `u64::MAX`.
21    #[error("measurement counter overflow: {metric:?}")]
22    CounterOverflow {
23        /// Counter that could not represent the next value.
24        metric: Metric,
25    },
26    /// An event or query moved behind the current record time.
27    #[error("measurement clock regressed from {current:?} to {observed:?}")]
28    ClockRegression {
29        /// Timestamp supplied by the adapter.
30        observed: UnixTime,
31        /// Latest timestamp or aligned epoch already represented by the record.
32        current: UnixTime,
33    },
34    /// A live record was used with a different reliability window.
35    #[error(
36        "reliability window changed from {stored_seconds}s to {supplied_seconds}s without migration"
37    )]
38    ReliabilityWindowMismatch {
39        /// Window stored with the retained reliability epoch.
40        stored_seconds: u64,
41        /// Window supplied by the caller for this operation.
42        supplied_seconds: u64,
43    },
44    /// A snapshot uses a schema version this crate cannot interpret.
45    #[error("unsupported measurement snapshot version {found}")]
46    UnsupportedSnapshotVersion {
47        /// Unrecognized schema version.
48        found: u16,
49    },
50    /// A snapshot contains more peer records than the configured ledger bound.
51    #[error("measurement snapshot has {found} peers, exceeding limit {max}")]
52    SnapshotPeerLimitExceeded {
53        /// Number of records encoded in the snapshot.
54        found: usize,
55        /// Configured maximum number of retained peer records.
56        max: usize,
57    },
58    /// A snapshot contains more than one record for the same peer key.
59    #[error("measurement snapshot contains a duplicate peer")]
60    DuplicatePeerInSnapshot,
61    /// A snapshot stores reliability evidence without a timestamped epoch.
62    #[error("measurement snapshot has reliability evidence without an epoch")]
63    SnapshotEvidenceWithoutEpoch,
64    /// A snapshot stores an epoch even though its reliability bucket is empty.
65    #[error("measurement snapshot has an empty timestamped reliability epoch")]
66    SnapshotEpochWithoutEvidence,
67    /// A snapshot reliability epoch has no associated window policy.
68    #[error("measurement snapshot reliability epoch has no window policy")]
69    SnapshotReliabilityWindowMissing,
70    /// A snapshot stores a reliability policy without an observed epoch.
71    #[error("measurement snapshot has a reliability window without an epoch")]
72    SnapshotReliabilityWindowWithoutEpoch,
73    /// A snapshot reliability epoch is not aligned to its stored window.
74    #[error("measurement snapshot reliability epoch is not window-aligned")]
75    SnapshotReliabilityEpochMisaligned,
76}