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: zerompk::Error,
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    /// Segment is encrypted (starts with `SEGV`) but no KEK was supplied.
91    #[error(
92        "columnar segment is encrypted but no encryption key was provided; \
93         cannot read an encrypted segment without a key"
94    )]
95    MissingKek,
96
97    /// Segment is plaintext (`NDBS`) but a KEK is configured (policy violation).
98    #[error(
99        "columnar segment is plaintext but an encryption key is configured; \
100         refusing to load an unencrypted segment when encryption is required"
101    )]
102    KekRequired,
103
104    /// AES-256-GCM encryption of the segment payload failed.
105    #[error("columnar segment encryption failed: {0}")]
106    EncryptionFailed(String),
107
108    /// AES-256-GCM decryption of the segment payload failed.
109    #[error("columnar segment decryption failed: {0}")]
110    DecryptionFailed(String),
111
112    /// Memory budget exhausted or global ceiling exceeded.
113    #[error("memory budget exhausted: {0}")]
114    BudgetExhausted(#[from] nodedb_mem::MemError),
115}