pub struct TypedErr<E> { /* private fields */ }Expand description
A typed wrapper around a user error that provides Deref access to the inner error type.
This struct holds an Arc<anyhow::Error> internally and provides safe access to
the downcasted error reference. The Arc ensures the error remains valid for the
lifetime of this wrapper.
§Example
use std::fmt;
use query_flow::{query, Db, QueryError, QueryResultExt, QueryRuntime};
#[derive(Debug)]
struct MyError {
code: u32,
}
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "my error {}", self.code)
}
}
impl std::error::Error for MyError {}
#[query]
fn failing(db: &impl Db) -> Result<i32, QueryError> {
let _ = db;
Err(MyError { code: 42 }.into())
}
#[query]
fn caller(db: &impl Db) -> Result<String, QueryError> {
let result = db.query(Failing::new()).downcast_err::<MyError>()?;
Ok(match result {
Ok(value) => format!("success: {}", value),
Err(typed_err) => {
// typed_err derefs to &MyError
format!("Error code: {}", typed_err.code)
}
})
}
let runtime = QueryRuntime::new();
assert_eq!(*runtime.query(Caller::new()).unwrap(), "Error code: 42");Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<E> Freeze for TypedErr<E>where
PhantomData<E>: Freeze,
impl<E> RefUnwindSafe for TypedErr<E>where
PhantomData<E>: RefUnwindSafe,
impl<E> Send for TypedErr<E>where
PhantomData<E>: Send,
impl<E> Sync for TypedErr<E>where
PhantomData<E>: Sync,
impl<E> Unpin for TypedErr<E>where
PhantomData<E>: Unpin,
impl<E> UnsafeUnpin for TypedErr<E>where
PhantomData<E>: UnsafeUnpin,
impl<E> UnwindSafe for TypedErr<E>where
PhantomData<E>: UnwindSafe,
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