Skip to main content

fcb_core/
error.rs

1use crate::packed_rtree::Error as PackedRtreeError;
2use cjseq::error::CjseqError;
3use flatbuffers::InvalidFlatbuffer;
4use serde_json;
5use thiserror::Error;
6
7/// The main error type for the FCB Core library.
8/// This enum represents all possible errors that can occur during FCB operations.
9#[derive(Debug, Error)]
10pub enum Error {
11    // File format errors
12    #[error("Missing magic bytes in FCB file header")]
13    MissingMagicBytes,
14
15    #[error("Required index is missing")]
16    NoIndex,
17
18    #[error("Attribute index not found")]
19    AttributeIndexNotFound,
20
21    #[error("Attribute index size overflow")]
22    AttributeIndexSizeOverflow,
23
24    #[error("No columns found in header")]
25    NoColumnsInHeader,
26
27    #[error("Missing required field of CityJSON: {0}")]
28    MissingRequiredField(String),
29
30    #[error("Invalid header size {0}, expected size between 8 and 1MB")]
31    IllegalHeaderSize(usize),
32
33    #[error("Invalid FlatBuffer format: {0}")]
34    InvalidFlatbuffer(#[from] InvalidFlatbuffer),
35
36    // IO and serialization errors
37    #[error("IO error: {0}")]
38    IoError(#[from] std::io::Error),
39
40    #[error("JSON error: {0}")]
41    JsonError(#[from] serde_json::Error),
42
43    #[error("R-tree error: {0}")]
44    RtreeError(#[from] PackedRtreeError),
45
46    // Validation errors
47    #[error("Unsupported column type: {0}")]
48    UnsupportedColumnType(String),
49
50    #[error("Invalid attribute value: {msg}")]
51    InvalidAttributeValue { msg: String },
52
53    // Index and query errors
54    #[error("Failed to create index: {0}")]
55    IndexCreationError(String),
56
57    #[error("Failed to execute query: {0}")]
58    QueryExecutionError(String),
59
60    // HTTP errors (when http feature is enabled)
61    #[cfg(feature = "http")]
62    #[error("HTTP client error: {0}")]
63    HttpClient(#[from] http_range_client::HttpError),
64
65    // CityJSON specific errors
66    #[error("CityJSON error: {source}")]
67    CityJson {
68        #[from]
69        source: crate::cjerror::CjError,
70    },
71
72    #[error("Cjseq error: {source}")]
73    CjseqError {
74        #[from]
75        source: CjseqError,
76    },
77
78    #[error("StaticBTree error: {source}")]
79    StaticBTree {
80        #[from]
81        source: crate::static_btree::Error,
82    },
83}
84
85impl Error {
86    /// Returns true if the error is related to IO operations
87    pub fn is_io_error(&self) -> bool {
88        matches!(self, Error::IoError(_))
89    }
90
91    /// Returns true if the error is related to data format
92    pub fn is_format_error(&self) -> bool {
93        matches!(
94            self,
95            Error::MissingMagicBytes | Error::InvalidFlatbuffer(_) | Error::IllegalHeaderSize(_)
96        )
97    }
98
99    /// Returns true if the error is related to validation
100    pub fn is_validation_error(&self) -> bool {
101        matches!(
102            self,
103            Error::UnsupportedColumnType(_) | Error::InvalidAttributeValue { .. }
104        )
105    }
106
107    /// Returns true if the error is related to index or query operations
108    pub fn is_index_error(&self) -> bool {
109        matches!(
110            self,
111            Error::IndexCreationError(_) | Error::QueryExecutionError(_)
112        )
113    }
114}
115
116pub type Result<T> = std::result::Result<T, Error>;