flare_core/error/
error.rs

1
2use thiserror::Error;
3use tokio_tungstenite::tungstenite;
4use crate::flare_net::net::{ResCode, Response};
5
6pub type Result<T> = std::result::Result<T, FlareErr>;
7
8
9#[derive(Debug, Error)]
10pub enum FlareErr {
11    #[error("error: {0}")]
12    Error(String),
13
14    #[error("invalid params `{0}`")]
15    InvalidParams(String),
16
17    #[error("connection error: {0}")]
18    ConnectionError(String),
19
20    #[error("business error: {0}")]
21    BusinessError(String),
22
23    #[error("args error: {0}")]
24    ArgsError(String),
25
26    #[error("Connection closed")]
27    ConnectionClosed,
28
29    /// 链接不存在
30    #[error("connection not found")]
31    ConnectionNotFound,
32
33    #[error("Failed to decode message: {0}")]
34    DecodeError(#[from] prost::DecodeError),
35
36    #[error("Failed to encode message: {0}")]
37    EncodeError(#[from] prost::EncodeError),
38
39    #[error("WebSocket error: {0}")]
40    WebSocketError(String),
41
42    #[error("Invalid message type")]
43    InvalidMessageType,
44
45    #[error("Protocol error: {0}")]
46    ProtocolError(String),
47
48    #[error("Authentication error: {0}")]
49    AuthError(String),
50
51    #[error(transparent)]
52    Other(#[from] anyhow::Error),
53
54    // 业务相关错误
55    #[error("not fond handler")]
56    NotFondHandler,
57
58    #[error("push to client error `{0}`")]
59    PushToClientErr(String),
60
61    #[error("send message err  code `{0}` msg `{1}`")]
62    SendMsgErr(i32, String),
63
64    // 命令相关错误
65    #[error("invalid command: {0}")]
66    InvalidCommand(String),
67
68    // 认证相关错误
69    #[error("unauthorized: {0}")]
70    Unauthorized(String),
71
72    // 内部错误
73    #[error("internal error: {0}")]
74    InternalError(String),
75
76    // 状态错误
77    #[error("invalid state: {0}")]
78    InvalidState(String),
79
80    // 超时错误
81    #[error("timeout: {0}")]
82    Timeout(String),
83
84    // 资源错误
85    #[error("resource error: {0}")]
86    ResourceError(String),
87
88    #[error("service not found: {0}")]
89    ServiceNotFound(String),
90
91}
92// 推送到客户端产生错误
93pub struct FlareError {
94    pub code: i32,
95    pub err: FlareErr,
96}
97
98impl FlareErr {
99    // 获取错误码
100    pub fn code(&self) -> ResCode {
101        match self {
102            FlareErr::Error(_) => ResCode::UnknownCode,
103            FlareErr::ConnectionError(_) => ResCode::ConnectionError,
104            FlareErr::BusinessError(_) => ResCode::BusinessError,
105            FlareErr::ArgsError(_) => ResCode::ArgsError,
106            FlareErr::InvalidParams(_) => ResCode::InvalidParams,
107            FlareErr::ConnectionClosed => ResCode::ConnectionClosed,
108            FlareErr::ConnectionNotFound => ResCode::ConnectionNotFound,
109            FlareErr::DecodeError(_) => ResCode::DecodeError,
110            FlareErr::EncodeError(_) => ResCode::EncodeError,
111            _ => ResCode::UnknownCode,
112        }
113    }
114
115    // 转换为响应
116    pub fn to_res(&self) -> Response {
117        Response {
118            code: self.code() as i32,
119            message: self.to_string(),
120            data: Vec::new(),
121        }
122    }
123
124    // 错误转换辅助方法
125    pub fn from_str(s: impl Into<String>) -> Self {
126        FlareErr::Error(s.into())
127    }
128
129    pub fn invalid_params(msg: impl Into<String>) -> Self {
130        FlareErr::InvalidParams(msg.into())
131    }
132
133    pub fn invalid_command(msg: impl Into<String>) -> Self {
134        FlareErr::InvalidCommand(msg.into())
135    }
136
137    pub fn unauthorized(msg: impl Into<String>) -> Self {
138        FlareErr::Unauthorized(msg.into())
139    }
140
141    pub fn internal_error(msg: impl Into<String>) -> Self {
142        FlareErr::InternalError(msg.into())
143    }
144
145    pub fn invalid_state(msg: impl Into<String>) -> Self {
146        FlareErr::InvalidState(msg.into())
147    }
148
149    pub fn timeout(msg: impl Into<String>) -> Self {
150        FlareErr::Timeout(msg.into())
151    }
152
153    pub fn resource_error(msg: impl Into<String>) -> Self {
154        FlareErr::ResourceError(msg.into())
155    }
156
157    pub fn not_found_service(msg: impl Into<String>) -> Self {
158        FlareErr::ServiceNotFound(msg.into())
159    }
160
161    pub fn connection_error(msg: impl Into<String>) -> Self {
162        FlareErr::ConnectionError(msg.into())
163    }
164
165    pub fn decode_error(err: prost::DecodeError) -> Self {
166        FlareErr::DecodeError(err)
167    }
168}
169
170// 实现 From trait 用于错误转换
171impl From<String> for FlareErr {
172    fn from(s: String) -> Self {
173        FlareErr::Error(s)
174    }
175}
176
177impl From<&str> for FlareErr {
178    fn from(s: &str) -> Self {
179        FlareErr::Error(s.to_string())
180    }
181}
182
183impl From<tungstenite::Error> for FlareErr {
184    fn from(err: tungstenite::Error) -> Self {
185        FlareErr::WebSocketError(err.to_string())
186    }
187}
188
189impl From<FlareErr> for ResCode {
190    fn from(err: FlareErr) -> Self {
191        match err {
192            FlareErr::Error(_) => ResCode::UnknownCode,
193            FlareErr::ConnectionError(_) => ResCode::ConnectionError,
194            FlareErr::BusinessError(_) => ResCode::BusinessError,
195            FlareErr::ArgsError(_) => ResCode::ArgsError,
196            FlareErr::InvalidParams(_) => ResCode::InvalidParams,
197            FlareErr::ConnectionClosed => ResCode::ConnectionClosed,
198            FlareErr::ConnectionNotFound => ResCode::ConnectionNotFound,
199            FlareErr::DecodeError(_) => ResCode::DecodeError,
200            FlareErr::EncodeError(_) => ResCode::EncodeError,
201            _ => ResCode::UnknownCode,
202        }
203    }
204}