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#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum EvaluationError {
15 #[error(transparent)]
17 Parsing(#[from] SparqlSyntaxError),
18 #[error(transparent)]
20 Storage(#[from] StorageError),
21 #[error(transparent)]
23 GraphParsing(#[from] RdfParseError),
24 #[error(transparent)]
26 ResultsParsing(#[from] ResultsParseError),
27 #[error(transparent)]
29 ResultsSerialization(io::Error),
30 #[error("{0}")]
32 Service(#[source] Box<dyn Error + Send + Sync + 'static>),
33 #[error("The graph {0} already exists")]
35 GraphAlreadyExists(NamedNode),
36 #[error("The graph {0} does not exist")]
38 GraphDoesNotExist(NamedNode),
39 #[error("The variable encoding the service name is unbound")]
41 UnboundService,
42 #[error("The service {0} is not supported")]
44 UnsupportedService(NamedNode),
45 #[error("The content media type {0} is not supported")]
47 UnsupportedContentType(String),
48 #[error("The service is not returning solutions but a boolean or a graph")]
50 ServiceDoesNotReturnSolutions,
51 #[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}