1use std::any::TypeId;
4use std::error::Error;
5use std::fmt;
6
7use crate::component::LifecyclePhase;
8
9#[derive(Debug)]
11pub enum ComponentError {
12    TypeNotFound(TypeId),
14
15    PropsMismatch { expected: TypeId, got: TypeId },
17
18    InvalidPropsType,
20
21    InvalidProps(String),
23
24    DowncastError,
26
27    LockError(String),
29
30    InvalidLifecycleTransition(LifecyclePhase, String),
32
33    RenderError(String),
35
36    UpdateError(String),
38
39    MountError(String),
41
42    UnmountError(String),
44
45    ReactiveSystemError(String),
47}
48
49impl fmt::Display for ComponentError {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            Self::TypeNotFound(type_id) => write!(f, "Component type {type_id:?} not found"),
53            Self::PropsMismatch { expected, got } => write!(
54                f,
55                "Props type mismatch - expected {expected:?}, got {got:?}"
56            ),
57            Self::InvalidPropsType => write!(f, "Invalid props type for the component"),
58            Self::DowncastError => write!(f, "Failed to downcast props or component"),
59            Self::LockError(msg) => write!(f, "Lock error: {msg}"),
60            Self::InvalidLifecycleTransition(phase, operation) => write!(
61                f,
62                "Invalid lifecycle transition: cannot {operation} while in {phase:?} phase"
63            ),
64            Self::RenderError(msg) => write!(f, "Error rendering component: {msg}"),
65            Self::UpdateError(msg) => write!(f, "Error updating component: {msg}"),
66            Self::MountError(msg) => write!(f, "Error mounting component: {msg}"),
67            Self::UnmountError(msg) => write!(f, "Error unmounting component: {msg}"),
68            Self::ReactiveSystemError(msg) => write!(f, "Reactive system error: {msg}"),
69            Self::InvalidProps(msg) => write!(f, "Invalid props: {msg}"),
70        }
71    }
72}
73
74impl Error for ComponentError {}
75
76impl From<crate::state::SignalError> for ComponentError {
78    fn from(error: crate::state::SignalError) -> Self {
79        ComponentError::ReactiveSystemError(error.to_string())
80    }
81}