Skip to main content

oxilite_core/
error.rs

1use std::fmt;
2
3/// Result alias used across oxilite.
4pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6/// Errors raised by oxilite.
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    /// The SPARQL query or update could not be parsed.
10    #[error(transparent)]
11    Syntax(#[from] spargebra::SparqlSyntaxError),
12    /// An RDF document could not be parsed.
13    #[error(transparent)]
14    Parse(#[from] oxrdfio::RdfParseError),
15    /// The SQL backend failed.
16    #[error("SQL backend error: {0}")]
17    Backend(String),
18    /// The operation needs something the compiler or backend cannot do.
19    #[error("unsupported: {0}")]
20    Unsupported(String),
21    /// Two different terms hashed to the same id.
22    #[error("term hash collision: {0}")]
23    Collision(String),
24    /// The database content is not what oxilite expects.
25    #[error("corrupted store: {0}")]
26    Corrupted(String),
27    /// Evaluation by the fallback evaluator failed.
28    #[error(transparent)]
29    Evaluation(#[from] spareval::QueryEvaluationError),
30    /// I/O error while reading or writing RDF.
31    #[error(transparent)]
32    Io(#[from] std::io::Error),
33    /// Other errors.
34    #[error("{0}")]
35    Other(String),
36}
37
38impl Error {
39    pub fn backend(e: impl fmt::Display) -> Self {
40        let msg = e.to_string();
41        if msg.contains("oxilite: term hash collision") {
42            Self::Collision(msg)
43        } else if msg.contains("graph_does_not_exist") {
44            Self::Other("the graph does not exist".into())
45        } else if msg.contains("graph_already_exists") {
46            Self::Other("the graph already exists".into())
47        } else if msg.contains("vector_dimensions_mismatch") {
48            Self::Other(
49                "a value of the indexed property is not a JSON array with the index's dimensions"
50                    .into(),
51            )
52        } else if msg.contains("computed_value_not_storable") {
53            Self::Unsupported(
54                "an update template stores a computed non-integer value, which SQL cannot hash"
55                    .into(),
56            )
57        } else {
58            Self::Backend(msg)
59        }
60    }
61
62    pub fn unsupported(msg: impl Into<String>) -> Self {
63        Self::Unsupported(msg.into())
64    }
65
66    pub fn corrupted(msg: impl Into<String>) -> Self {
67        Self::Corrupted(msg.into())
68    }
69
70    /// Is this error a request for the fallback evaluator?
71    pub fn is_unsupported(&self) -> bool {
72        matches!(self, Self::Unsupported(_))
73    }
74}