use super::*;
impl ErrorBoundary {
pub fn new() -> Self {
Self {
phase: Signal::create(ErrorBoundaryPhase::Healthy),
}
}
pub fn try_with<F, R>(&self, closure: F) -> Result<R, String>
where
F: FnOnce() -> R + UnwindSafe,
{
match catch_unwind(closure) {
Ok(value) => Ok(value),
Err(payload) => {
let message: String = extract_message(&payload);
let _ = catch_unwind(AssertUnwindSafe(|| {
self.get_phase()
.set(ErrorBoundaryPhase::Caught(message.clone()));
}));
Err(message)
}
}
}
pub fn report_error(&self, message: &str) -> String {
let owned: String = String::from(message);
let _ = catch_unwind(AssertUnwindSafe(|| {
self.get_phase()
.set(ErrorBoundaryPhase::Caught(owned.clone()));
}));
owned
}
pub fn reset(&self) {
let _ = catch_unwind(AssertUnwindSafe(|| {
self.get_phase().set(ErrorBoundaryPhase::Healthy);
}));
}
}
impl Default for ErrorBoundary {
fn default() -> Self {
Self::new()
}
}
impl Display for ErrorBoundary {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
write!(formatter, "ErrorBoundary({:?})", self.get_phase().get())
}
}
impl PartialEq for ErrorBoundaryPhase {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(ErrorBoundaryPhase::Healthy, ErrorBoundaryPhase::Healthy) => true,
(ErrorBoundaryPhase::Caught(a), ErrorBoundaryPhase::Caught(b)) => a == b,
_ => false,
}
}
}