1use std::fmt;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6#[derive(Debug, thiserror::Error)]
8pub enum Error {
9 #[error(transparent)]
11 Syntax(#[from] spargebra::SparqlSyntaxError),
12 #[error(transparent)]
14 Parse(#[from] oxrdfio::RdfParseError),
15 #[error("SQL backend error: {0}")]
17 Backend(String),
18 #[error("unsupported: {0}")]
20 Unsupported(String),
21 #[error("term hash collision: {0}")]
23 Collision(String),
24 #[error("corrupted store: {0}")]
26 Corrupted(String),
27 #[error(transparent)]
29 Evaluation(#[from] spareval::QueryEvaluationError),
30 #[error(transparent)]
32 Io(#[from] std::io::Error),
33 #[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 pub fn is_unsupported(&self) -> bool {
72 matches!(self, Self::Unsupported(_))
73 }
74}