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(
9        "invalid index name `{0}`: must be non-empty; no whitespace or `/` `\\` `*` `?` `,` `+` `\"` `'` `;` or backtick; \
10         must not start with `.` `-` or `_`"
11    )]
12    InvalidIndexName(String),
13    /// 刻意**不带**原始 host:带 userinfo 的地址一旦回显,密码就换个地方泄漏。
14    #[error("invalid host: {0}")]
15    InvalidHost(String),
16    #[error("invalid field name `{0}`: only letters, digits, `_`, `-` and `.` are allowed")]
17    InvalidFieldName(String),
18    #[error("invalid search result: {0}")]
19    InvalidResult(String),
20    #[error("unsupported operation: {0}")]
21    Unsupported(String),
22    #[error("JSON error: {0}")]
23    Json(#[from] serde_json::Error),
24    #[cfg(any(
25        feature = "elasticsearch",
26        feature = "meilisearch",
27        feature = "typesense",
28        feature = "algolia"
29    ))]
30    #[error("HTTP request failed: {0}")]
31    Http(#[from] reqwest::Error),
32    #[cfg(feature = "database")]
33    #[error("SQLite error: {0}")]
34    Sqlx(#[from] sqlx::Error),
35    #[cfg(any(
36        feature = "elasticsearch",
37        feature = "meilisearch",
38        feature = "typesense",
39        feature = "algolia",
40        feature = "xunsearch"
41    ))]
42    #[error("backend engine request failed: {0}")]
43    Backend(String),
44    #[cfg(feature = "xunsearch")]
45    #[error("xunsearch error: {0}")]
46    XunSearch(String),
47    #[cfg(feature = "xunsearch")]
48    #[error("xunsearch I/O error: {0}")]
49    XunSearchIo(#[from] std::io::Error),
50}
51
52impl ScoutError {
53    /// 项目宠物「检索机器人 Scout」针对这个错误的排查提示。
54    ///
55    /// 每个错误变体对应一句人话方向,例如索引名非法时会提示命名规则、
56    /// `Unsupported` 时会提示检查 feature。渲染成完整文本用
57    /// [`crate::pet::format_error`]。
58    pub fn pet_hint(&self) -> &'static str {
59        crate::pet::hint(self)
60    }
61}
62
63pub type Result<T> = std::result::Result<T, ScoutError>;