Skip to main content

ng_oxigraph/oxigraph/storage/
error.rs

1use crate::oxigraph::io::{RdfFormat, RdfParseError};
2use crate::oxigraph::storage::numeric_encoder::EncodedTerm;
3use crate::oxrdf::TermRef;
4use oxiri::IriParseError;
5use std::error::Error;
6use std::io;
7
8/// An error related to storage operations (reads, writes...).
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum StorageError {
12    /// Error from the OS I/O layer.
13    #[error(transparent)]
14    Io(#[from] io::Error),
15    /// Error related to data corruption.
16    #[error(transparent)]
17    Corruption(#[from] CorruptionError),
18    #[doc(hidden)]
19    #[error("{0}")]
20    Other(#[source] Box<dyn Error + Send + Sync + 'static>),
21}
22
23impl From<StorageError> for io::Error {
24    #[inline]
25    fn from(error: StorageError) -> Self {
26        match error {
27            StorageError::Io(error) => error,
28            StorageError::Corruption(error) => error.into(),
29            StorageError::Other(error) => Self::new(io::ErrorKind::Other, error),
30        }
31    }
32}
33
34/// An error return if some content in the database is corrupted.
35#[derive(Debug, thiserror::Error)]
36#[error(transparent)]
37pub struct CorruptionError(#[from] CorruptionErrorKind);
38
39/// An error return if some content in the database is corrupted.
40#[derive(Debug, thiserror::Error)]
41enum CorruptionErrorKind {
42    #[error("{0}")]
43    Msg(String),
44    #[error("{0}")]
45    Other(#[source] Box<dyn Error + Send + Sync + 'static>),
46}
47
48impl CorruptionError {
49    /// Builds an error from a printable error message.
50    #[inline]
51    pub(crate) fn new(error: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
52        Self(CorruptionErrorKind::Other(error.into()))
53    }
54
55    #[inline]
56    pub(crate) fn from_encoded_term(encoded: &EncodedTerm, term: &TermRef<'_>) -> Self {
57        // TODO: eventually use a dedicated error enum value
58        Self::msg(format!("Invalid term encoding {encoded:?} for {term}"))
59    }
60
61    #[inline]
62    pub(crate) fn from_missing_column_family_name(name: &'static str) -> Self {
63        // TODO: eventually use a dedicated error enum value
64        Self::msg(format!("Column family {name} does not exist"))
65    }
66
67    /// Builds an error from a printable error message.
68    #[inline]
69    pub(crate) fn msg(msg: impl Into<String>) -> Self {
70        Self(CorruptionErrorKind::Msg(msg.into()))
71    }
72}
73
74impl From<CorruptionError> for io::Error {
75    #[inline]
76    fn from(error: CorruptionError) -> Self {
77        Self::new(io::ErrorKind::InvalidData, error)
78    }
79}
80
81/// An error raised while loading a file into a [`Store`](crate::oxigraph::store::Store).
82#[derive(Debug, thiserror::Error)]
83pub enum LoaderError {
84    /// An error raised while reading the file.
85    #[error(transparent)]
86    Parsing(#[from] RdfParseError),
87    /// An error raised during the insertion in the store.
88    #[error(transparent)]
89    Storage(#[from] StorageError),
90    /// The base IRI is invalid.
91    #[error("Invalid base IRI '{iri}': {error}")]
92    InvalidBaseIri {
93        /// The IRI itself.
94        iri: String,
95        /// The parsing error.
96        #[source]
97        error: IriParseError,
98    },
99}
100
101impl From<LoaderError> for io::Error {
102    #[inline]
103    fn from(error: LoaderError) -> Self {
104        match error {
105            LoaderError::Storage(error) => error.into(),
106            LoaderError::Parsing(error) => error.into(),
107            LoaderError::InvalidBaseIri { .. } => {
108                Self::new(io::ErrorKind::InvalidInput, error.to_string())
109            }
110        }
111    }
112}
113
114/// An error raised while writing a file from a [`Store`](crate::oxigraph::store::Store).
115#[derive(Debug, thiserror::Error)]
116pub enum SerializerError {
117    /// An error raised while writing the content.
118    #[error(transparent)]
119    Io(#[from] io::Error),
120    /// An error raised during the lookup in the store.
121    #[error(transparent)]
122    Storage(#[from] StorageError),
123    /// A format compatible with [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) is required.
124    #[error("A RDF format supporting datasets was expected, {0} found")]
125    DatasetFormatExpected(RdfFormat),
126}
127
128impl From<SerializerError> for io::Error {
129    #[inline]
130    fn from(error: SerializerError) -> Self {
131        match error {
132            SerializerError::Storage(error) => error.into(),
133            SerializerError::Io(error) => error,
134            SerializerError::DatasetFormatExpected(_) => {
135                Self::new(io::ErrorKind::InvalidInput, error.to_string())
136            }
137        }
138    }
139}