waratah_registry/error.rs
1//! Typed string-registry errors.
2
3use std::path::PathBuf;
4
5use thiserror::Error;
6use tonic::Status;
7use waratah_proto::ProtoError;
8use waratah_storage::{StorageError, StrHash};
9
10/// Failure serving a `StringRegistry` RPC.
11#[derive(Debug, Error)]
12pub enum RegistryError {
13 /// Registry shard failure.
14 #[error(transparent)]
15 Storage(#[from] StorageError),
16
17 /// Malformed request payload (for example a short `StrHash`).
18 #[error(transparent)]
19 Proto(#[from] ProtoError),
20
21 /// A registered string exceeds [`crate::MAX_REGISTERED_STRING_BYTES`].
22 ///
23 /// Enforced on IRIs and literal lexical forms alike: the dictionary's
24 /// job is RDF terms, not blobs, and multi-megabyte values are bad for
25 /// both the gRPC message ceiling and LSM compaction.
26 #[error(
27 "registered string is {len} UTF-8 bytes, over the {max} byte limit",
28 max = crate::MAX_REGISTERED_STRING_BYTES
29 )]
30 StringTooLarge {
31 /// Offending string's UTF-8 byte length.
32 len: usize,
33 },
34
35 /// The registry mutex was poisoned by a panicking request.
36 #[error("registry lock poisoned")]
37 LockPoisoned,
38
39 /// `PutBlobChunks` / `PutBlobs` / `GetBlobs` against a registry with no
40 /// blob store attached (a server-configuration state, not a
41 /// client-input error).
42 #[error("this registry has no blob store attached")]
43 BlobStoreMissing,
44
45 /// A blob exceeds the 64 MiB [`crate::blobs::MAX_BLOB_BYTES`] limit.
46 #[error("blob is {len} bytes, over the {max} byte limit")]
47 BlobTooLarge {
48 /// Offending payload byte length.
49 len: usize,
50 /// Configured maximum.
51 max: usize,
52 },
53
54 /// A blob payload was empty. Empty payloads chunk to zero frames on
55 /// the streamed read path (reading back as a miss), and real payloads
56 /// always have at least a header, so the store rejects them at the
57 /// write boundary.
58 #[error("blob {id} has an empty payload")]
59 BlobEmpty {
60 /// Offending blob's content hash.
61 id: StrHash,
62 },
63
64 /// A `PutBlobs` request carried no blobs.
65 #[error("PutBlobs request carried no blobs")]
66 EmptyBlobBatch,
67
68 /// A blob payload does not hash to the id it was stored under.
69 #[error("blob {id} rejected: payload does not hash to its blob_id")]
70 BlobHashMismatch {
71 /// Content hash the caller claimed for the payload.
72 id: StrHash,
73 },
74
75 /// A stored blob value or segment record failed an integrity check
76 /// (undecodable index value, record/locator disagreement, CRC
77 /// mismatch, missing or truncated segment). Distinct from a miss:
78 /// corruption is loud.
79 #[error("blob store corruption: {reason}")]
80 BlobCorrupt {
81 /// What is wrong, naming the affected blob/segment where known.
82 reason: String,
83 },
84
85 /// The data dir still has the old one-file-per-payload blob layout.
86 /// The store refuses to open instead of serving an empty index.
87 #[error(
88 "legacy per-file blob layout at {path}; re-import into a fresh data dir (the old `blobs/{{xx}}/{{hash}}.bin` files are not read)"
89 )]
90 LegacyBlobLayout {
91 /// Blob store root holding the legacy layout.
92 path: PathBuf,
93 },
94
95 /// A blob-store filesystem operation failed outside the RocksDB wrapper
96 /// (directory creation, legacy-layout probe).
97 #[error("blob store I/O failed: {0}")]
98 BlobIo(String),
99
100 /// Listener setup failure.
101 #[error("listen failed: {0}")]
102 Listen(#[from] std::io::Error),
103
104 /// gRPC transport failure while serving.
105 #[error("transport error: {0}")]
106 Transport(#[from] tonic::transport::Error),
107}
108
109impl From<RegistryError> for Status {
110 fn from(error: RegistryError) -> Self {
111 let message = error.to_string();
112 match error {
113 RegistryError::Proto(_)
114 | RegistryError::StringTooLarge { .. }
115 | RegistryError::BlobTooLarge { .. }
116 | RegistryError::BlobEmpty { .. }
117 | RegistryError::EmptyBlobBatch
118 | RegistryError::BlobHashMismatch { .. } => Self::invalid_argument(message),
119 // No blob store attached is a server-configuration state, not a
120 // bad request.
121 RegistryError::BlobStoreMissing => Self::unavailable(message),
122 // A legacy-layout data dir predates the two-tier store; it
123 // must be re-imported before this store can serve.
124 RegistryError::LegacyBlobLayout { .. } => Self::failed_precondition(message),
125 _ => Self::internal(message),
126 }
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use tonic::Code;
133
134 use super::*;
135
136 #[test]
137 fn blob_store_missing_maps_to_unavailable() {
138 let status = Status::from(RegistryError::BlobStoreMissing);
139 assert_eq!(status.code(), Code::Unavailable);
140 }
141
142 #[test]
143 fn blob_empty_maps_to_invalid_argument() {
144 let error = RegistryError::BlobEmpty {
145 id: StrHash::new("http://ex/empty"),
146 };
147 let status = Status::from(error);
148 assert_eq!(status.code(), Code::InvalidArgument);
149 }
150}