Skip to main content

spareval/
error.rs

1use crate::expression::ExpressionEvaluationError;
2use oxrdf::{NamedNode, Term, Variable};
3use spargebra::SparqlSyntaxError;
4use std::convert::Infallible;
5use std::error::Error;
6use std::ops::RangeInclusive;
7
8/// A SPARQL evaluation error
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum QueryEvaluationError {
12    /// Error from the underlying RDF dataset
13    #[error(transparent)]
14    Dataset(Box<dyn Error + Send + Sync>),
15    /// Error during `SERVICE` evaluation
16    #[error("{0}")]
17    Service(#[source] Box<dyn Error + Send + Sync>),
18    /// If a variable present in the given initial substitution is not present in the `SELECT` part of the query
19    #[error("The SPARQL query does not contains variable {0} in its SELECT projection")]
20    NotExistingSubstitutedVariable(Variable),
21    /// Error if the dataset returns the default graph even if a named graph is expected
22    #[error("The SPARQL dataset returned the default graph even if a named graph is expected")]
23    UnexpectedDefaultGraph,
24    /// The given custom function is not supported
25    #[error("The custom function {0} is not supported")]
26    UnsupportedCustomFunction(NamedNode),
27    /// The given custom function arity is not supported
28    #[error("The custom function {name} requires between {} and {} arguments, but {actual} were given", .expected.start(), .expected.end())]
29    UnsupportedCustomFunctionArity {
30        name: NamedNode,
31        expected: RangeInclusive<usize>,
32        actual: usize,
33    },
34    /// The variable storing the `SERVICE` name is unbound
35    #[error("The variable encoding the service name is unbound")]
36    UnboundService,
37    /// Invalid service name
38    #[error("{0} is not a valid service name")]
39    InvalidServiceName(Term),
40    /// The given `SERVICE` is not supported
41    #[error("The service {0} is not supported")]
42    UnsupportedService(NamedNode),
43    #[cfg(feature = "sparql-12")]
44    #[error("The SPARQL dataset returned a triple term that is not a valid RDF 1.2 term")]
45    InvalidStorageTripleTerm,
46    #[error("The SPARQL operation has been cancelled")]
47    Cancelled,
48    #[doc(hidden)]
49    #[error(transparent)]
50    Unexpected(Box<dyn Error + Send + Sync>),
51}
52
53impl From<Infallible> for QueryEvaluationError {
54    #[inline]
55    fn from(error: Infallible) -> Self {
56        match error {}
57    }
58}
59
60// TODO: remove when removing the Store::update method
61#[doc(hidden)]
62impl From<SparqlSyntaxError> for QueryEvaluationError {
63    #[inline]
64    fn from(error: SparqlSyntaxError) -> Self {
65        Self::Unexpected(Box::new(error))
66    }
67}
68
69impl From<ExpressionEvaluationError<Self>> for QueryEvaluationError {
70    #[inline]
71    fn from(error: ExpressionEvaluationError<Self>) -> Self {
72        match error {
73            ExpressionEvaluationError::Context(e) => e,
74            ExpressionEvaluationError::UnsupportedCustomFunction(name) => {
75                Self::UnsupportedCustomFunction(name)
76            }
77            ExpressionEvaluationError::UnsupportedCustomFunctionArity {
78                name,
79                expected,
80                actual,
81            } => Self::UnsupportedCustomFunctionArity {
82                name,
83                expected,
84                actual,
85            },
86        }
87    }
88}