Skip to main content

nodedb_columnar/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Error types for columnar segment operations.
4
5/// Errors from columnar segment encoding, decoding, and validation.
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum ColumnarError {
9    #[error("codec error: {0}")]
10    Codec(#[from] nodedb_codec::CodecError),
11
12    #[error("segment too short: expected at least {expected} bytes, got {got}")]
13    TruncatedSegment { expected: usize, got: usize },
14
15    #[error("invalid magic bytes: expected NDBS, got {0:?}")]
16    InvalidMagic([u8; 4]),
17
18    #[error(
19        "incompatible segment version: reader {reader_major}.x, segment {segment_major}.{segment_minor}"
20    )]
21    IncompatibleVersion {
22        reader_major: u8,
23        segment_major: u8,
24        segment_minor: u8,
25    },
26
27    #[error("footer CRC32C mismatch: stored {stored:#010x}, computed {computed:#010x}")]
28    FooterCrcMismatch { stored: u32, computed: u32 },
29
30    #[error("column index {index} out of range (segment has {count} columns)")]
31    ColumnOutOfRange { index: usize, count: usize },
32
33    #[error("schema mismatch: expected {expected} columns, memtable has {got}")]
34    SchemaMismatch { expected: usize, got: usize },
35
36    #[error("memtable is empty — nothing to flush")]
37    EmptyMemtable,
38
39    #[error("serialization error: {0}")]
40    Serialization(String),
41
42    #[error("value type mismatch for column '{column}': expected {expected}")]
43    TypeMismatch { column: String, expected: String },
44
45    #[error("JSON parse error for column '{column}': {source}")]
46    JsonParse {
47        column: String,
48        source: sonic_rs::Error,
49    },
50
51    #[error("range literal parse error for column '{column}': invalid range literal '{literal}'")]
52    RangeParse { column: String, literal: String },
53
54    #[error("MessagePack serialization error for column '{column}': {source}")]
55    MsgpackSerialize {
56        column: String,
57        source: zerompk::Error,
58    },
59
60    #[error("MessagePack deserialization error for column '{column}': {source}")]
61    MsgpackDeserialize {
62        column: String,
63        source: nodedb_types::MsgpackError,
64    },
65
66    #[error("null violation: column '{0}' is NOT NULL")]
67    NullViolation(String),
68
69    #[error("duplicate primary key")]
70    DuplicatePrimaryKey,
71
72    #[error("primary key not found")]
73    PrimaryKeyNotFound,
74
75    #[error("segment ID space exhausted: u64::MAX segments have been allocated")]
76    SegmentIdExhausted,
77
78    /// Structural corruption detected while parsing a segment header or footer.
79    ///
80    /// `offset` is the byte position from the start of the segment where the
81    /// problem was detected. `segment_id` is `None` when the parse fails before
82    /// the id can be resolved (e.g. inside the footer itself).
83    #[error("segment corruption in {segment_id:?} at offset {offset:?}: {reason}")]
84    Corruption {
85        segment_id: Option<String>,
86        reason: String,
87        offset: Option<u64>,
88    },
89
90    /// A memtable cell whose bytes do not hold a value of its declared type.
91    ///
92    /// The memtable writer encodes every cell it stores, so this is in-memory
93    /// corruption. The read is refused rather than answered with `NULL`.
94    #[error("memtable cell corrupt: column '{column}' row {row}: {reason}")]
95    MemtableCellCorrupt {
96        column: String,
97        row: usize,
98        reason: String,
99    },
100
101    /// A columnar WAL row whose bytes do not decode.
102    ///
103    /// `offset` is the byte position in the row where decoding stopped. The
104    /// decode is refused rather than answered with `NULL` or replacement
105    /// characters.
106    #[error("columnar WAL row corrupt at byte {offset}: {reason}")]
107    WalRowCorrupt { offset: usize, reason: String },
108
109    /// A string column cell whose bytes are not UTF-8 at segment write time.
110    ///
111    /// The memtable push stores only valid UTF-8, so these bytes were
112    /// damaged in memory. The segment write is refused rather than written
113    /// with wrong block statistics.
114    #[error("string column '{column}' row {row} holds bytes that are not UTF-8")]
115    StringCellNotUtf8 { column: String, row: usize },
116
117    /// Segment is encrypted (starts with `SEGV`) but no KEK was supplied.
118    #[error(
119        "columnar segment is encrypted but no encryption key was provided; \
120         cannot read an encrypted segment without a key"
121    )]
122    MissingKek,
123
124    /// Segment is plaintext (`NDBS`) but a KEK is configured (policy violation).
125    #[error(
126        "columnar segment is plaintext but an encryption key is configured; \
127         refusing to load an unencrypted segment when encryption is required"
128    )]
129    KekRequired,
130
131    /// AES-256-GCM encryption of the segment payload failed.
132    #[error("columnar segment encryption failed: {0}")]
133    EncryptionFailed(String),
134
135    /// AES-256-GCM decryption of the segment payload failed.
136    #[error("columnar segment decryption failed: {0}")]
137    DecryptionFailed(String),
138
139    /// Memory budget exhausted or global ceiling exceeded.
140    #[error("memory budget exhausted: {0}")]
141    BudgetExhausted(#[from] nodedb_mem::MemError),
142}