1use std::fmt;
8
9#[derive(Debug)]
11pub enum SearchError {
12 Http(reqwest::Error),
16 Json(serde_json::Error),
18 Status(u16),
20 Api(String),
22 Captcha,
24 Payload(String),
26 InvalidConfig(String),
28}
29
30impl fmt::Display for SearchError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Self::Http(error) => write!(f, "HTTP 请求失败:{error}"),
34 Self::Json(error) => write!(f, "响应 JSON 解析失败:{error}"),
35 Self::Status(status) => write!(f, "服务端返回 HTTP {status}"),
36 Self::Api(message) => write!(f, "平台返回错误:{message}"),
37 Self::Captcha => write!(f, "命中验证码风控,需要人工处理"),
38 Self::Payload(message) => write!(f, "响应内容解析失败:{message}"),
39 Self::InvalidConfig(message) => write!(f, "配置非法:{message}"),
40 }
41 }
42}
43
44impl std::error::Error for SearchError {
45 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46 match self {
47 Self::Http(error) => Some(error),
48 Self::Json(error) => Some(error),
49 _ => None,
50 }
51 }
52}
53
54impl From<reqwest::Error> for SearchError {
55 fn from(error: reqwest::Error) -> Self {
56 Self::Http(error)
57 }
58}
59
60impl From<serde_json::Error> for SearchError {
61 fn from(error: serde_json::Error) -> Self {
62 Self::Json(error)
63 }
64}