1use std::fmt;
2
3#[derive(Debug)]
16pub enum Error {
17 Config(String),
19 Plugin {
21 name: String,
23 source: Box<dyn std::error::Error + Send + Sync>,
25 },
26 Server(String),
28 Io(std::io::Error),
30 Template(String),
32 Msg(String),
34 Api(crate::api::ApiError),
36}
37
38impl fmt::Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match self {
41 Error::Config(msg) => write!(f, "配置错误: {msg}"),
42 Error::Plugin { name, source } => write!(f, "插件 [{name}] 错误: {source}"),
43 Error::Server(msg) => write!(f, "服务器错误: {msg}"),
44 Error::Io(e) => write!(f, "IO 错误: {e}"),
45 Error::Template(msg) => write!(f, "模板错误: {msg}"),
46 Error::Msg(msg) => write!(f, "{msg}"),
47 Error::Api(e) => write!(f, "API 错误: {e}"),
48 }
49 }
50}
51
52impl std::error::Error for Error {
53 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54 match self {
55 Error::Io(e) => Some(e),
56 Error::Plugin { source, .. } => Some(source.as_ref()),
57 Error::Api(e) => Some(e),
58 _ => None,
59 }
60 }
61}
62
63impl From<std::io::Error> for Error {
64 fn from(e: std::io::Error) -> Self {
65 Error::Io(e)
66 }
67}
68
69impl From<String> for Error {
70 fn from(s: String) -> Self {
71 Error::Msg(s)
72 }
73}
74
75impl From<&str> for Error {
76 fn from(s: &str) -> Self {
77 Error::Msg(s.to_string())
78 }
79}
80
81impl From<crate::api::ApiError> for Error {
82 fn from(e: crate::api::ApiError) -> Self {
83 Error::Api(e)
85 }
86}
87
88pub type Result<T, E = Error> = std::result::Result<T, E>;