oxigraph/sparql/
error.rs

1use crate::io::RdfParseError;
2use crate::model::NamedNode;
3use crate::sparql::results::QueryResultsParseError as ResultsParseError;
4use crate::sparql::SparqlSyntaxError;
5use crate::store::{CorruptionError, StorageError};
6use spareval::QueryEvaluationError;
7use std::convert::Infallible;
8use std::error::Error;
9use std::io;
10
11/// A SPARQL evaluation error.
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum EvaluationError {
15    /// An error in SPARQL parsing.
16    #[error(transparent)]
17    Parsing(#[from] SparqlSyntaxError),
18    /// An error from the storage.
19    #[error(transparent)]
20    Storage(#[from] StorageError),
21    /// An error while parsing an external RDF file.
22    #[error(transparent)]
23    GraphParsing(#[from] RdfParseError),
24    /// An error while parsing an external result file (likely from a federated query).
25    #[error(transparent)]
26    ResultsParsing(#[from] ResultsParseError),
27    /// An error returned during results serialization.
28    #[error(transparent)]
29    ResultsSerialization(io::Error),
30    /// Error during `SERVICE` evaluation
31    #[error("{0}")]
32    Service(#[source] Box<dyn Error + Send + Sync + 'static>),
33    /// Error when `CREATE` tries to create an already existing graph
34    #[error("The graph {0} already exists")]
35    GraphAlreadyExists(NamedNode),
36    /// Error when `DROP` or `CLEAR` tries to remove a not existing graph
37    #[error("The graph {0} does not exist")]
38    GraphDoesNotExist(NamedNode),
39    /// The variable storing the `SERVICE` name is unbound
40    #[error("The variable encoding the service name is unbound")]
41    UnboundService,
42    /// The given `SERVICE` is not supported
43    #[error("The service {0} is not supported")]
44    UnsupportedService(NamedNode),
45    /// The given content media type returned from an HTTP response is not supported (`SERVICE` and `LOAD`)
46    #[error("The content media type {0} is not supported")]
47    UnsupportedContentType(String),
48    /// The `SERVICE` call has not returns solutions
49    #[error("The service is not returning solutions but a boolean or a graph")]
50    ServiceDoesNotReturnSolutions,
51    /// The results are not a RDF graph
52    #[error("The query results are not a RDF graph")]
53    NotAGraph,
54    #[doc(hidden)]
55    #[error(transparent)]
56    Unexpected(Box<dyn Error + Send + Sync>),
57}
58
59impl From<Infallible> for EvaluationError {
60    #[inline]
61    fn from(error: Infallible) -> Self {
62        match error {}
63    }
64}
65
66impl From<QueryEvaluationError> for EvaluationError {
67    fn from(error: QueryEvaluationError) -> Self {
68        match error {
69            QueryEvaluationError::Dataset(error) => match error.downcast() {
70                Ok(error) => Self::Storage(*error),
71                Err(error) => Self::Unexpected(error),
72            },
73            QueryEvaluationError::Service(error) => Self::Service(error),
74            QueryEvaluationError::UnexpectedDefaultGraph => Self::Storage(
75                CorruptionError::new("Unexpected default graph in SPARQL results").into(),
76            ),
77            e => Self::Storage(
78                CorruptionError::new(format!("Unsupported SPARQL evaluation error: {e}")).into(),
79            ),
80        }
81    }
82}
83
84impl From<EvaluationError> for io::Error {
85    #[inline]
86    fn from(error: EvaluationError) -> Self {
87        match error {
88            EvaluationError::Parsing(error) => Self::new(io::ErrorKind::InvalidData, error),
89            EvaluationError::GraphParsing(error) => error.into(),
90            EvaluationError::ResultsParsing(error) => error.into(),
91            EvaluationError::ResultsSerialization(error) => error,
92            EvaluationError::Storage(error) => error.into(),
93            EvaluationError::Service(error) | EvaluationError::Unexpected(error) => {
94                match error.downcast() {
95                    Ok(error) => *error,
96                    Err(error) => Self::other(error),
97                }
98            }
99            EvaluationError::GraphAlreadyExists(_)
100            | EvaluationError::GraphDoesNotExist(_)
101            | EvaluationError::UnboundService
102            | EvaluationError::UnsupportedService(_)
103            | EvaluationError::UnsupportedContentType(_)
104            | EvaluationError::ServiceDoesNotReturnSolutions
105            | EvaluationError::NotAGraph => Self::new(io::ErrorKind::InvalidInput, error),
106        }
107    }
108}