summa_core/
errors.rs

1use std::convert::{From, Infallible};
2use std::fmt::{Display, Formatter};
3use std::path::PathBuf;
4
5use tantivy::schema::FieldType;
6
7#[derive(thiserror::Error, Debug)]
8pub enum ValidationError {
9    #[error("builder_error: {0}")]
10    Builder(#[from] BuilderError),
11    #[error("invalid_fast_field_type_error: ({field:?}, {field_type:?}, {tantivy_error:?})")]
12    InvalidFastFieldType {
13        field: String,
14        field_type: FieldType,
15        tantivy_error: tantivy::TantivyError,
16    },
17    #[error("invalid_http_header: <{0}: {1}>")]
18    InvalidHttpHeader(String, String),
19    #[error("invalid_segments_number: {0}")]
20    InvalidSegmentsNumber(u32),
21    #[error("invalid_schema_error: {0}")]
22    InvalidSchema(String),
23    #[error("invalid_unique_field_type_error: {0:?}")]
24    InvalidUniqueFieldType(FieldType),
25    #[error("empty_argument_error: {0}")]
26    EmptyArgument(String),
27    #[error("existing_path_error: {0}")]
28    ExistingPath(PathBuf),
29    #[error("missing_index_error: {0}")]
30    MissingIndex(String),
31    #[error("missing_field_error: {0}")]
32    MissingField(String),
33    #[error("missing_header_error: {0}")]
34    MissingHeader(String),
35    #[error("missing_path_error: {0}")]
36    MissingPath(PathBuf),
37    #[error("missing_range")]
38    MissingRange,
39    #[error("missing_unique_field_error: {0:?}")]
40    MissingUniqueField(String),
41    #[error("required_fast_field: {0}")]
42    RequiredFastField(String),
43    #[error("utf8_error: {0}")]
44    Utf8(#[from] std::str::Utf8Error),
45    #[error("template_error: {0}")]
46    Template(#[from] strfmt::FmtError),
47}
48
49#[derive(thiserror::Error, Debug)]
50pub enum Error {
51    #[error("addr_parse_error: {0}")]
52    AddrParse(#[from] std::net::AddrParseError),
53    #[error("anyhow_error: {0}")]
54    Anyhow(#[from] anyhow::Error),
55    #[error("config_error: {0}")]
56    Config(#[from] config::ConfigError),
57    #[error("document_parsing_error: {0}")]
58    DocumentParsing(#[from] crate::components::DocumentParsingError),
59    #[error("empty_query_error")]
60    EmptyQuery,
61    #[error("fast_eval_error: {0:?}")]
62    FastEval(#[from] fasteval2::Error),
63    #[cfg(feature = "external-request")]
64    #[error("hyper_error: {0}")]
65    Hyper(#[from] hyper::Error),
66    #[cfg(feature = "external-request")]
67    #[error("hyper_http_error: {0}")]
68    HyperHttp(#[from] hyper::http::Error),
69    #[error("infallible")]
70    Infallible,
71    #[error("internal_error")]
72    Internal,
73    #[error("invalid_aggregation")]
74    InvalidAggregation,
75    #[error("{0:?}: {1:?}")]
76    InvalidFieldType(String, FieldType),
77    #[error("{0:?} for {1:?}")]
78    InvalidQuerySyntax(Box<crate::components::QueryParserError>, String),
79    #[error("{0:?}")]
80    InvalidSegmentId(String),
81    #[error("{0:?}")]
82    InvalidSyntax(String),
83    #[error("{0:?}")]
84    IO((std::io::Error, Option<PathBuf>)),
85    #[error("json_error: {0}")]
86    Json(#[from] serde_json::Error),
87    #[error("open_directory_error: {0}")]
88    OpenDirectory(#[from] tantivy::directory::error::OpenDirectoryError),
89    #[error("tantivy_error: {0}")]
90    Tantivy(#[from] tantivy::TantivyError),
91    #[error("read_only_index: {0}")]
92    ReadOnlyIndex(String),
93    #[error("request_error: {0}")]
94    RequestError(#[from] crate::directories::RequestError),
95    #[error("unbound_document_error")]
96    UnboundDocument,
97    #[error("unknown_directory_error: {0}")]
98    UnknownDirectory(String),
99    #[error("{0}")]
100    Validation(Box<ValidationError>),
101    #[error("{0}")]
102    Yaml(#[from] serde_yaml::Error),
103}
104
105#[derive(thiserror::Error, Debug)]
106pub enum BuilderError {
107    /// Uninitialized field
108    UninitializedField(&'static str),
109    /// Custom validation error
110    ValidationError(String),
111}
112
113impl From<BuilderError> for Error {
114    fn from(error: BuilderError) -> Self {
115        Error::Validation(Box::new(ValidationError::Builder(error)))
116    }
117}
118
119impl From<ValidationError> for Error {
120    fn from(error: ValidationError) -> Self {
121        Error::Validation(Box::new(error))
122    }
123}
124
125#[cfg(feature = "tokio-rt")]
126impl From<tokio::task::JoinError> for Error {
127    fn from(_error: tokio::task::JoinError) -> Self {
128        Error::Internal
129    }
130}
131
132impl From<std::io::Error> for Error {
133    fn from(error: std::io::Error) -> Self {
134        Error::IO((error, None))
135    }
136}
137
138impl From<Infallible> for Error {
139    fn from(_: Infallible) -> Self {
140        Error::Infallible
141    }
142}
143
144impl From<Error> for std::io::Error {
145    fn from(error: Error) -> Self {
146        std::io::Error::new(std::io::ErrorKind::Other, error)
147    }
148}
149
150impl Display for BuilderError {
151    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
152        match self {
153            BuilderError::UninitializedField(s) => write!(f, "UninitializedField({s})"),
154            BuilderError::ValidationError(s) => write!(f, "ValidationError({s})"),
155        }
156    }
157}
158
159impl From<String> for BuilderError {
160    fn from(s: String) -> Self {
161        Self::ValidationError(s)
162    }
163}
164
165impl From<derive_builder::UninitializedFieldError> for BuilderError {
166    fn from(error: derive_builder::UninitializedFieldError) -> Self {
167        Self::ValidationError(error.to_string())
168    }
169}
170
171pub type SummaResult<T> = Result<T, Error>;