1use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum NcmError {
6 #[error("HTTP request failed: {0}")]
8 Http(#[from] reqwest::Error),
9
10 #[error("API error (code={code}): {msg}")]
12 Api { code: i64, msg: String },
13
14 #[error("Authentication required: {0}")]
16 AuthRequired(String),
17
18 #[error("Invalid parameter: {0}")]
20 InvalidParam(String),
21
22 #[error("Crypto error: {0}")]
24 Crypto(String),
25
26 #[error("JSON error: {0}")]
28 Json(#[from] serde_json::Error),
29
30 #[error("Request timeout: {0}")]
32 Timeout(String),
33
34 #[error("Rate limited: {0}")]
36 RateLimited(String),
37
38 #[error("{0}")]
40 Unknown(String),
41}
42
43impl NcmError {
44 pub fn from_api(code: i64, msg: String) -> Self {
46 match code {
47 301 => NcmError::AuthRequired(if msg.is_empty() {
48 "需要登录".to_string()
49 } else {
50 msg
51 }),
52 400 => NcmError::InvalidParam(msg),
53 503 => NcmError::RateLimited(msg),
54 _ => NcmError::Api { code, msg },
55 }
56 }
57}
58
59pub type Result<T> = std::result::Result<T, NcmError>;