pub struct PanicRecovery;Expand description
Panic recovery utilities for safer error handling
Implementations§
Source§impl PanicRecovery
impl PanicRecovery
Sourcepub fn catch_unwind<F, T>(f: F) -> ChieResult<T>where
F: FnOnce() -> T + UnwindSafe,
pub fn catch_unwind<F, T>(f: F) -> ChieResult<T>where
F: FnOnce() -> T + UnwindSafe,
Catch panics and convert them to ChieError
§Errors
Returns ChieError with ErrorKind::Internal if the function panics
§Examples
use chie_shared::{PanicRecovery, ErrorKind};
let result = PanicRecovery::catch_unwind(|| {
// This would normally panic
if false {
panic!("Something went wrong!");
}
42
});
assert!(result.is_ok());
assert_eq!(result.unwrap(), 42);Sourcepub fn catch_unwind_with_context<F, T>(
f: F,
context: impl Into<String>,
) -> ChieResult<T>where
F: FnOnce() -> T + UnwindSafe,
pub fn catch_unwind_with_context<F, T>(
f: F,
context: impl Into<String>,
) -> ChieResult<T>where
F: FnOnce() -> T + UnwindSafe,
Catch panics with custom error context
§Errors
Returns ChieError with the provided context if the function panics
Sourcepub fn retry_on_panic<F, T>(max_attempts: usize, f: F) -> ChieResult<T>where
F: FnMut() -> T + UnwindSafe,
pub fn retry_on_panic<F, T>(max_attempts: usize, f: F) -> ChieResult<T>where
F: FnMut() -> T + UnwindSafe,
Retry a function that might panic, up to max_attempts times
§Errors
Returns ChieError if all attempts fail with panics
§Examples
use chie_shared::PanicRecovery;
use std::sync::atomic::{AtomicUsize, Ordering};
let attempt = AtomicUsize::new(0);
let result = PanicRecovery::retry_on_panic(3, || {
let current = attempt.fetch_add(1, Ordering::SeqCst) + 1;
if current < 3 {
panic!("Not yet!");
}
"success"
});
assert!(result.is_ok());Sourcepub fn with_barrier<F, T>(f: F, fallback: T) -> T
pub fn with_barrier<F, T>(f: F, fallback: T) -> T
Execute function with panic barrier - isolates panics from caller
Auto Trait Implementations§
impl Freeze for PanicRecovery
impl RefUnwindSafe for PanicRecovery
impl Send for PanicRecovery
impl Sync for PanicRecovery
impl Unpin for PanicRecovery
impl UnwindSafe for PanicRecovery
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more