Enum ServerFnError

Source
pub enum ServerFnError<T = String> {
    ServerError(T),
    CommunicationError(ServerFnErrorErr),
}
Expand description

An error type for server functions. This may either be an error that occurred while running the server function logic, or an error that occurred while communicating with the server inside the server function crate.

§Usage

You can use the ServerFnError type in the Error type of your server function result or use the ServerFnResult type as the return type of your server function. When you call the server function, you can handle the error directly or convert it into a CapturedError to throw into the nearest ErrorBoundary.

use dioxus::prelude::*;

#[server]
async fn parse_number(number: String) -> ServerFnResult<f32> {
    // You can convert any error type into the `ServerFnError` with the `?` operator
    let parsed_number: f32 = number.parse()?;
    Ok(parsed_number)
}

#[component]
fn ParseNumberServer() -> Element {
    let mut number = use_signal(|| "42".to_string());
    let mut parsed = use_signal(|| None);

    rsx! {
        input {
            value: "{number}",
            oninput: move |e| number.set(e.value()),
        }
        button {
            onclick: move |_| async move {
                // Call the server function to parse the number
                // If the result is Ok, continue running the closure, otherwise bubble up the
                // error to the nearest error boundary with `?`
                let result = parse_number(number()).await?;
                parsed.set(Some(result));
                Ok(())
            },
            "Parse Number"
        }
        if let Some(value) = parsed() {
            p { "Parsed number: {value}" }
        } else {
            p { "No number parsed yet." }
        }
    }
}

§Differences from CapturedError

Both this error type and CapturedError can be used to represent boxed errors in dioxus. However, this error type is more strict about the kinds of errors it can represent. CapturedError can represent any error that implements the Error trait or can be converted to a string. CapturedError holds onto the type information of the error and lets you downcast the error to its original type.

ServerFnError represents server function errors as Strings by default without any additional type information. This makes it easy to serialize the error to JSON and send it over the wire, but it means that you can’t get the original type information of the error back. If you need to preserve the type information of the error, you can use a custom error variant that holds onto the type information.

§Custom error variants

The ServerFnError type accepts a generic type parameter T that is used to represent the error type used for server functions. If you need to keep the type information of your error, you can create a custom error variant that implements Serialize and DeserializeOwned. This allows you to serialize the error to JSON and send it over the wire, while still preserving the type information.

use dioxus::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MyCustomError {
    message: String,
    code: u32,
}

impl MyCustomError {
    pub fn new(message: String, code: u32) -> Self {
        Self { message, code }
    }
}

#[server]
async fn server_function() -> ServerFnResult<String, MyCustomError> {
    // Return your custom error
    Err(ServerFnError::ServerError(MyCustomError::new(
        "An error occurred".to_string(),
        404,
    )))
}

Variants§

§

ServerError(T)

An error running the server function

§

CommunicationError(ServerFnErrorErr)

An error communicating with the server

Implementations§

Source§

impl ServerFnError

Source

pub fn new(error: impl ToString) -> Self

Creates a new ServerFnError from something that implements ToString.

§Examples
use dioxus::prelude::*;
use serde::{Serialize, Deserialize};

#[server]
async fn server_function() -> ServerFnResult<String> {
    // Return your custom error
    Err(ServerFnError::new("Something went wrong"))
}
Source§

impl<T> ServerFnError<T>

Source

pub fn is_server_error(&self) -> bool

Returns true if the error is a server error

Source

pub fn is_communication_error(&self) -> bool

Returns true if the error is a communication error

Source

pub fn server_error(&self) -> Option<&T>

Returns a reference to the server error if it is a server error or None if it is a communication error.

Source

pub fn communication_error(&self) -> Option<&ServerFnErrorErr>

Returns a reference to the communication error if it is a communication error or None if it is a server error.

Trait Implementations§

Source§

impl<T: Clone> Clone for ServerFnError<T>

Source§

fn clone(&self) -> ServerFnError<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ServerFnError<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for ServerFnError<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Display> Display for ServerFnError<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E: Error> From<E> for ServerFnError

Source§

fn from(error: E) -> Self

Converts to this type from the input type.
Source§

impl From<ServerFnError> for CapturedError

Source§

fn from(error: ServerFnError) -> Self

Converts to this type from the input type.
Source§

impl From<ServerFnError> for RenderError

Source§

fn from(error: ServerFnError) -> Self

Converts to this type from the input type.
Source§

impl<T: Serialize + DeserializeOwned + Debug + 'static> FromServerFnError for ServerFnError<T>

Source§

type Encoder = JsonEncoding

The encoding strategy used to serialize and deserialize this error type. Must implement the Encodes trait for references to the error type.
Source§

fn from_server_fn_error(err: ServerFnErrorErr) -> Self

Converts a ServerFnErrorErr into the application-specific custom error type.
Source§

fn ser(&self) -> Bytes

Converts the custom error type to a String.
Source§

fn de(data: Bytes) -> Self

Deserializes the custom error type from a &str.
Source§

impl<T: FromStr> FromStr for ServerFnError<T>

Source§

type Err = <T as FromStr>::Err

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T: PartialEq> PartialEq for ServerFnError<T>

Source§

fn eq(&self, other: &ServerFnError<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for ServerFnError<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for ServerFnError<T>

Source§

impl<T> StructuralPartialEq for ServerFnError<T>

Auto Trait Implementations§

§

impl<T> Freeze for ServerFnError<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ServerFnError<T>
where T: RefUnwindSafe,

§

impl<T> Send for ServerFnError<T>
where T: Send,

§

impl<T> Sync for ServerFnError<T>
where T: Sync,

§

impl<T> Unpin for ServerFnError<T>
where T: Unpin,

§

impl<T> UnwindSafe for ServerFnError<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<E, T, Request> FromReq<DeleteUrl, Request, E> for T
where Request: Req<E> + Send + 'static, T: DeserializeOwned, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request> FromReq<GetUrl, Request, E> for T
where Request: Req<E> + Send + 'static, T: DeserializeOwned, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request, Encoding> FromReq<Patch<Encoding>, Request, E> for T
where Request: Req<E> + Send + 'static, Encoding: Decodes<T>, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request> FromReq<PatchUrl, Request, E> for T
where Request: Req<E> + Send + 'static, T: DeserializeOwned, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request, Encoding> FromReq<Post<Encoding>, Request, E> for T
where Request: Req<E> + Send + 'static, Encoding: Decodes<T>, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request> FromReq<PostUrl, Request, E> for T
where Request: Req<E> + Send + 'static, T: DeserializeOwned, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request, Encoding> FromReq<Put<Encoding>, Request, E> for T
where Request: Req<E> + Send + 'static, Encoding: Decodes<T>, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, T, Request> FromReq<PutUrl, Request, E> for T
where Request: Req<E> + Send + 'static, T: DeserializeOwned, E: FromServerFnError,

Source§

async fn from_req(req: Request) -> Result<T, E>

Attempts to deserialize the arguments from a request.
Source§

impl<E, Encoding, Response, T> FromRes<Patch<Encoding>, Response, E> for T
where Response: ClientRes<E> + Send, Encoding: Decodes<T>, E: FromServerFnError,

Source§

async fn from_res(res: Response) -> Result<T, E>

Attempts to deserialize the outputs from a response.
Source§

impl<E, Encoding, Response, T> FromRes<Post<Encoding>, Response, E> for T
where Response: ClientRes<E> + Send, Encoding: Decodes<T>, E: FromServerFnError,

Source§

async fn from_res(res: Response) -> Result<T, E>

Attempts to deserialize the outputs from a response.
Source§

impl<E, Encoding, Response, T> FromRes<Put<Encoding>, Response, E> for T
where Response: ClientRes<E> + Send, Encoding: Decodes<T>, E: FromServerFnError,

Source§

async fn from_res(res: Response) -> Result<T, E>

Attempts to deserialize the outputs from a response.
Source§

impl<T> InitializeFromFunction<T> for T

Source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<E, T, Request> IntoReq<DeleteUrl, Request, E> for T
where Request: ClientReq<E>, T: Serialize + Send, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Request> IntoReq<GetUrl, Request, E> for T
where Request: ClientReq<E>, T: Serialize + Send, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Encoding, Request> IntoReq<Patch<Encoding>, Request, E> for T
where Request: ClientReq<E>, Encoding: Encodes<T>, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Request> IntoReq<PatchUrl, Request, E> for T
where Request: ClientReq<E>, T: Serialize + Send, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Encoding, Request> IntoReq<Post<Encoding>, Request, E> for T
where Request: ClientReq<E>, Encoding: Encodes<T>, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Request> IntoReq<PostUrl, Request, E> for T
where Request: ClientReq<E>, T: Serialize + Send, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Encoding, Request> IntoReq<Put<Encoding>, Request, E> for T
where Request: ClientReq<E>, Encoding: Encodes<T>, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, T, Request> IntoReq<PutUrl, Request, E> for T
where Request: ClientReq<E>, T: Serialize + Send, E: FromServerFnError,

Source§

fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>

Attempts to serialize the arguments into an HTTP request.
Source§

impl<E, Response, Encoding, T> IntoRes<Patch<Encoding>, Response, E> for T
where Response: TryRes<E>, Encoding: Encodes<T>, E: FromServerFnError + Send, T: Send,

Source§

async fn into_res(self) -> Result<Response, E>

Attempts to serialize the output into an HTTP response.
Source§

impl<E, Response, Encoding, T> IntoRes<Post<Encoding>, Response, E> for T
where Response: TryRes<E>, Encoding: Encodes<T>, E: FromServerFnError + Send, T: Send,

Source§

async fn into_res(self) -> Result<Response, E>

Attempts to serialize the output into an HTTP response.
Source§

impl<E, Response, Encoding, T> IntoRes<Put<Encoding>, Response, E> for T
where Response: TryRes<E>, Encoding: Encodes<T>, E: FromServerFnError + Send, T: Send,

Source§

async fn into_res(self) -> Result<Response, E>

Attempts to serialize the output into an HTTP response.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<Ret> SpawnIfAsync<(), Ret> for Ret

Source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
Source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

Source§

fn super_from(input: T) -> O

Convert from a type to another type.
Source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

Source§

fn super_into(self) -> O

Convert from a type to another type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DependencyElement for T
where T: 'static + PartialEq + Clone,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,