PanicRecovery

Struct PanicRecovery 

Source
pub struct PanicRecovery;
Expand description

Panic recovery utilities for safer error handling

Implementations§

Source§

impl PanicRecovery

Source

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);
Source

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

Source

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());
Source

pub fn with_barrier<F, T>(f: F, fallback: T) -> T
where F: FnOnce() -> T + UnwindSafe, T: Clone,

Execute function with panic barrier - isolates panics from caller

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.