Skip to main content

rust_scout/
error.rs

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