Skip to main content

waratah_partition/
error.rs

1//! Typed partition-service errors.
2
3use thiserror::Error;
4use tonic::Status;
5use waratah_proto::ProtoError;
6use waratah_stats::StatsError;
7use waratah_storage::{IndexPermutation, PartitionAxis, StorageError};
8
9/// Failure serving a `Hexastore` RPC on a single partition axis.
10#[derive(Debug, Error)]
11pub enum PartitionError {
12    /// The request asserted an axis this process does not own.
13    #[error("request targets partition axis {requested}, but this process owns {owned}")]
14    AxisMismatch {
15        /// Axis named by the caller.
16        requested: PartitionAxis,
17        /// Axis this process was started for.
18        owned: PartitionAxis,
19    },
20
21    /// Storage-brick failure, including a request naming a foreign index.
22    #[error(transparent)]
23    Storage(#[from] StorageError),
24
25    /// Sketch / stats-profile failure (Phase 7).
26    #[error(transparent)]
27    Stats(#[from] StatsError),
28
29    /// O-axis text-index engine failure.
30    #[error(transparent)]
31    Text(#[from] waratah_text_index::TextIndexError),
32
33    /// Partition-state lock poisoned.
34    #[error("partition state lock poisoned")]
35    LockPoisoned,
36
37    /// Malformed request payload.
38    #[error(transparent)]
39    Proto(#[from] ProtoError),
40
41    /// A `DomainScan` / `AxisHeadScan` named a permutation this axis does not
42    /// own (foreign axis ⇒ UNIMPLEMENTED, like a foreign `ScanPattern`).
43    #[error("{0} permutation is not owned by this process's axis ({1})")]
44    ScanPermutationNotOwned(&'static str, PartitionAxis),
45
46    /// The RPC does not serve this index permutation at all
47    /// (`TSG` under `DomainScan`; `SOPG` / `POSG` under `AxisHeadScan`).
48    #[error("{rpc} does not serve permutation {permutation:?}")]
49    UnsupportedPermutation {
50        /// RPC name for the message.
51        rpc: &'static str,
52        /// Rejected permutation.
53        permutation: IndexPermutation,
54    },
55
56    /// The RPC requires an explicit index permutation.
57    #[error("{0} requires an explicit index permutation")]
58    PermutationRequired(&'static str),
59
60    /// Domain constraint list exceeded the planner cap.
61    #[error("DomainScan constraint list exceeds the PUSHDOWN_TERMS_LIMIT terms limit ({0})")]
62    DomainScanTooLarge(usize),
63
64    /// A domain count mode was combined with an incompatible request flag.
65    #[error(
66        "DomainScanCounts count mode is incompatible with the request (DistinctGraphs requires merge_default_graph unset)"
67    )]
68    InvalidCountMode,
69
70    /// A text subject request combined `subject_equals_graph` with
71    /// `merge_default_graph`. The merge rewrites every row's graph to the
72    /// unnamed graph, so no subject can equal its graph; the equality
73    /// filter would drop the whole window, and the subject form has no
74    /// residual to recover those rows. Reject the request instead of
75    /// answering empty.
76    #[error(
77        "text subject request sets subject_equals_graph and merge_default_graph together; \
78         the merge removes the graph identity the equality test needs"
79    )]
80    SubjectEqualsGraphWithMerge,
81
82    /// Listener setup failure.
83    #[error("listen failed: {0}")]
84    Listen(#[from] std::io::Error),
85
86    /// gRPC transport failure while serving.
87    #[error("transport error: {0}")]
88    Transport(#[from] tonic::transport::Error),
89}
90
91impl From<PartitionError> for Status {
92    fn from(error: PartitionError) -> Self {
93        let message = error.to_string();
94        match error {
95            PartitionError::AxisMismatch { .. }
96            | PartitionError::Storage(StorageError::PermutationNotOwned { .. }) => {
97                Self::failed_precondition(message)
98            }
99            PartitionError::Storage(StorageError::CatalogNotOnAxis(_))
100            | PartitionError::ScanPermutationNotOwned(_, _) => Self::unimplemented(message),
101            PartitionError::Storage(StorageError::InvalidCatalogGraph(_))
102            | PartitionError::Storage(StorageError::Corrupt(_))
103            | PartitionError::UnsupportedPermutation { .. }
104            | PartitionError::PermutationRequired(_)
105            | PartitionError::DomainScanTooLarge(_)
106            | PartitionError::InvalidCountMode
107            | PartitionError::SubjectEqualsGraphWithMerge
108            | PartitionError::Proto(_) => Self::invalid_argument(message),
109            PartitionError::Text(
110                waratah_text_index::TextIndexError::CandidateLimitExceeded { .. }
111                | waratah_text_index::TextIndexError::SubjectLimitExceeded { .. },
112            ) => Self::resource_exhausted(message),
113            PartitionError::Text(
114                waratah_text_index::TextIndexError::SchemaMismatch
115                | waratah_text_index::TextIndexError::Config(_),
116            ) => Self::failed_precondition(message),
117            PartitionError::Text(waratah_text_index::TextIndexError::EmptyPattern) => {
118                Self::invalid_argument(message)
119            }
120            _ => Self::internal(message),
121        }
122    }
123}