maomi/
error.rs

1//! The error utilities.
2
3/// An framework error.
4#[derive(Debug)]
5pub enum Error {
6    /// A custom error.
7    ///
8    /// Generally refers to errors generated by other crates.
9    Custom(Box<dyn std::error::Error>),
10    /// The operation is invalid before component created.
11    TreeNotCreated,
12    /// A wrong node tree is visited.
13    ///
14    /// Generally refers to some bad operation had being done directly in the node tree.
15    TreeNodeTypeWrong,
16    /// The backend tree node has been released.
17    ///
18    /// Generally refers to some bad operation had being done directly in the node tree.
19    TreeNodeReleased,
20    /// A list update failed due to wrong changes list.
21    ///
22    /// Generally refers to some bad operation had being done directly in the node tree.
23    ListChangeWrong,
24    /// A recursive update is detected.
25    ///
26    /// An element cannot be updated while it is still in another update process.
27    /// This will not happen while calling async update methods like `ComponentRc::task` or `ComponentRc::update` .
28    /// Generally refers to some manually update process being incorrectly triggered.
29    RecursiveUpdate,
30    /// The backend context has already entered.
31    AlreadyEntered,
32    /// A general backend failure.
33    BackendError {
34        /// The message from backend.
35        msg: String,
36        /// The detailed backend error object.
37        err: Option<Box<dyn std::error::Error>>,
38    },
39}
40
41impl std::fmt::Display for Error {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Error::Custom(err) => {
45                write!(f, "{}", err)?;
46            }
47            Error::TreeNotCreated => {
48                write!(f, "Component template has not been initialized")?;
49            }
50            Error::TreeNodeTypeWrong => {
51                write!(f, "The node type in backend element tree is incorrect")?;
52            }
53            Error::TreeNodeReleased => {
54                write!(f, "The node in backend element tree has been released")?;
55            }
56            Error::ListChangeWrong => {
57                write!(f, "The list change is incorrect")?;
58            }
59            Error::RecursiveUpdate => {
60                write!(f, "A recursive update is detected")?;
61            }
62            Error::AlreadyEntered => {
63                write!(f, "The backend context is already entered")?;
64            }
65            Error::BackendError { msg, err } => {
66                if let Some(err) = err {
67                    write!(f, "{}: {}", msg, err.to_string())?;
68                } else {
69                    write!(f, "{}", msg)?;
70                }
71            }
72        }
73        Ok(())
74    }
75}
76
77impl std::error::Error for Error {}