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}
35
36impl fmt::Display for Error {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match self {
39 Error::Config(msg) => write!(f, "配置错误: {msg}"),
40 Error::Plugin { name, source } => write!(f, "插件 [{name}] 错误: {source}"),
41 Error::Server(msg) => write!(f, "服务器错误: {msg}"),
42 Error::Io(e) => write!(f, "IO 错误: {e}"),
43 Error::Template(msg) => write!(f, "模板错误: {msg}"),
44 Error::Msg(msg) => write!(f, "{msg}"),
45 }
46 }
47}
48
49impl std::error::Error for Error {
50 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
51 match self {
52 Error::Io(e) => Some(e),
53 Error::Plugin { source, .. } => Some(source.as_ref()),
54 _ => None,
55 }
56 }
57}
58
59impl From<std::io::Error> for Error {
60 fn from(e: std::io::Error) -> Self {
61 Error::Io(e)
62 }
63}
64
65impl From<String> for Error {
66 fn from(s: String) -> Self {
67 Error::Msg(s)
68 }
69}
70
71impl From<&str> for Error {
72 fn from(s: &str) -> Self {
73 Error::Msg(s.to_string())
74 }
75}
76
77impl From<crate::api::ApiError> for Error {
78 fn from(e: crate::api::ApiError) -> Self {
79 Error::Msg(e.msg)
80 }
81}
82
83pub type Result<T> = std::result::Result<T, Error>;