Trait Context

Source
pub trait Context<T, E>: Sealed {
    // Required methods
    fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T>;
    fn context<C: Display + 'static>(self, context: C) -> Result<T>;
    fn with_context<C: Display + 'static>(
        self,
        context: impl FnOnce() -> C,
    ) -> Result<T>;
}
Expand description

Provides context methods to Result and Option types that are compatible with CapturedError

This trait is sealed and cannot be implemented outside of dioxus-core

Required Methods§

Source

fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T>

Add a visual representation of the error that the ErrorBoundary may render

§Example
fn Component() -> Element {
    // You can bubble up errors with `?` inside components, and event handlers
    // Along with the error itself, you can provide a way to display the error by calling `show`
    let number = "1234".parse::<usize>().show(|error| rsx! {
        div {
            background_color: "red",
            color: "white",
            "Error parsing number: {error}"
        }
    })?;
    unimplemented!()
}
Source

fn context<C: Display + 'static>(self, context: C) -> Result<T>

Wrap the result additional context about the error that occurred.

§Example
fn NumberParser() -> Element {
    // You can bubble up errors with `?` inside components, and event handlers
    // Along with the error itself, you can provide a way to display the error by calling `context`
    let number = "-1234".parse::<usize>().context("Parsing number inside of the NumberParser")?;
    unimplemented!()
}
Source

fn with_context<C: Display + 'static>( self, context: impl FnOnce() -> C, ) -> Result<T>

Wrap the result with additional context about the error that occurred. The closure will only be run if the Result is an error.

§Example
fn NumberParser() -> Element {
    // You can bubble up errors with `?` inside components, and event handlers
    // Along with the error itself, you can provide a way to display the error by calling `context`
    let number = "-1234".parse::<usize>().with_context(|| format!("Timestamp: {:?}", std::time::Instant::now()))?;
    unimplemented!()
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> Context<T, CapturedError> for Option<T>

Source§

fn show( self, display_error: impl FnOnce(&CapturedError) -> Element, ) -> Result<T>

Source§

fn context<C: Display + 'static>(self, context: C) -> Result<T>

Source§

fn with_context<C: Display + 'static>( self, context: impl FnOnce() -> C, ) -> Result<T>

Source§

impl<T, E> Context<T, E> for Result<T, E>
where E: Error + 'static,

Source§

fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T>

Source§

fn context<C: Display + 'static>(self, context: C) -> Result<T>

Source§

fn with_context<C: Display + 'static>( self, context: impl FnOnce() -> C, ) -> Result<T>

Implementors§