Skip to main content

nectar_primitives/file/
error.rs

1//! Error types for file splitting and joining operations.
2
3use crate::ChunkAddress;
4use thiserror::Error;
5
6/// Errors from file splitting and joining operations.
7#[derive(Error, Debug)]
8pub enum FileError {
9    /// Write exceeded the declared span length.
10    #[error("write past span length: wrote {written} bytes, span is {span}")]
11    WritePastSpan {
12        /// Declared span length.
13        span: u64,
14        /// Bytes written so far.
15        written: u64,
16    },
17
18    /// Chunk data exceeds maximum allowed size.
19    #[error("chunk too large: max {max}, got {actual}")]
20    ChunkTooLarge {
21        /// Maximum allowed size.
22        max: usize,
23        /// Actual size.
24        actual: usize,
25    },
26
27    /// Chunk store failed to store a chunk.
28    #[error("store error: {0}")]
29    Store(Box<dyn std::error::Error + Send + Sync>),
30
31    /// Chunk getter failed to retrieve a chunk.
32    #[error("getter error: {0}")]
33    Getter(Box<dyn std::error::Error + Send + Sync>),
34
35    /// Invalid chunk reference encountered during tree traversal.
36    #[error("invalid reference at level {level}")]
37    InvalidReference {
38        /// Tree level where the invalid reference was found.
39        level: usize,
40    },
41
42    /// Required chunk was not found.
43    #[error("chunk not found: {0}")]
44    ChunkNotFound(ChunkAddress),
45
46    /// Span value doesn't match expected value.
47    #[error("span mismatch: expected {expected}, got {actual}")]
48    SpanMismatch {
49        /// Expected span value.
50        expected: u64,
51        /// Actual span value.
52        actual: u64,
53    },
54
55    /// Underlying chunk error.
56    #[error("chunk error: {0}")]
57    Chunk(#[from] crate::chunk::error::ChunkError),
58
59    /// Encryption error.
60    #[error("encryption error: {0}")]
61    Encryption(#[from] crate::chunk::encryption::EncryptionError),
62
63    /// Invalid entry reference length (expected 32 or 64 bytes).
64    #[error("invalid entry reference length: {len} (expected 32 or 64)")]
65    InvalidEntryRef {
66        /// Actual byte length of the reference.
67        len: usize,
68    },
69
70    /// Expected a content chunk but got a different chunk type.
71    #[error("expected content chunk, got {type_name}")]
72    InvalidChunkType {
73        /// Name of the chunk type that was received.
74        type_name: &'static str,
75    },
76}
77
78impl FileError {
79    /// Create a store error from any error type.
80    pub fn store<E: std::error::Error + Send + Sync + 'static>(err: E) -> Self {
81        Self::Store(Box::new(err))
82    }
83
84    /// Create a getter error from any error type.
85    pub fn getter<E: std::error::Error + Send + Sync + 'static>(err: E) -> Self {
86        Self::Getter(Box::new(err))
87    }
88}
89
90/// Result type for file operations.
91pub type Result<T> = std::result::Result<T, FileError>;