dioxus_lib::prelude

Trait Context

source
pub trait Context<T, E>: Sealed {
    // Required methods
    fn show(
        self,
        display_error: impl FnOnce(&E) -> Result<VNode, RenderError>,
    ) -> Result<T, CapturedError>;
    fn context<C>(self, context: C) -> Result<T, CapturedError>
       where C: Display + 'static;
    fn with_context<C>(
        self,
        context: impl FnOnce() -> C,
    ) -> Result<T, CapturedError>
       where C: Display + 'static;
}
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) -> Result<VNode, RenderError>, ) -> Result<T, CapturedError>

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>(self, context: C) -> Result<T, CapturedError>
where C: Display + 'static,

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>( self, context: impl FnOnce() -> C, ) -> Result<T, CapturedError>
where C: Display + 'static,

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!()
}

Object Safety§

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) -> Result<VNode, RenderError>, ) -> Result<T, CapturedError>

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

Implementors§