Skip to main content

rust_scout/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum ScoutError {
5    #[error("invalid index name `{0}`: must be non-empty, contain no whitespace, contain no '/', and not start with '.'")]
6    InvalidIndexName(String),
7    #[error("invalid search result: {0}")]
8    InvalidResult(String),
9    #[error("unsupported operation: {0}")]
10    Unsupported(String),
11    #[error("JSON error: {0}")]
12    Json(#[from] serde_json::Error),
13    #[cfg(any(
14        feature = "elasticsearch",
15        feature = "meilisearch",
16        feature = "typesense",
17        feature = "algolia"
18    ))]
19    #[error("HTTP request failed: {0}")]
20    Http(#[from] reqwest::Error),
21    #[cfg(feature = "database")]
22    #[error("SQLite error: {0}")]
23    Sqlx(#[from] sqlx::Error),
24    #[cfg(any(
25        feature = "elasticsearch",
26        feature = "meilisearch",
27        feature = "typesense",
28        feature = "algolia",
29        feature = "xunsearch"
30    ))]
31    #[error("backend engine request failed: {0}")]
32    Backend(String),
33    #[cfg(feature = "xunsearch")]
34    #[error("xunsearch error: {0}")]
35    XunSearch(String),
36    #[cfg(feature = "xunsearch")]
37    #[error("xunsearch I/O error: {0}")]
38    XunSearchIo(#[from] std::io::Error),
39}
40
41impl ScoutError {
42    /// 项目宠物「嗅探猎犬 Scout」针对这个错误的排查提示。
43    ///
44    /// 每个错误变体对应一句人话方向,例如索引名非法时会提示命名规则、
45    /// `Unsupported` 时会提示检查 feature。渲染成完整文本用
46    /// [`crate::pet::format_error`]。
47    pub fn pet_hint(&self) -> &'static str {
48        crate::pet::hint(self)
49    }
50}
51
52pub type Result<T> = std::result::Result<T, ScoutError>;