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
impl ServerFnError
Sourcepub fn new(error: impl ToString) -> Self
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>
impl<T> ServerFnError<T>
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Returns true if the error is a server error
Sourcepub fn is_communication_error(&self) -> bool
pub fn is_communication_error(&self) -> bool
Returns true if the error is a communication error
Sourcepub fn server_error(&self) -> Option<&T>
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.
Sourcepub fn communication_error(&self) -> Option<&ServerFnErrorErr>
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>
impl<T: Clone> Clone for ServerFnError<T>
Source§fn clone(&self) -> ServerFnError<T>
fn clone(&self) -> ServerFnError<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for ServerFnError<T>
impl<T: Debug> Debug for ServerFnError<T>
Source§impl<'de, T> Deserialize<'de> for ServerFnError<T>where
T: Deserialize<'de>,
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>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: Display> Display for ServerFnError<T>
impl<T: Display> Display for ServerFnError<T>
Source§impl<E: Error> From<E> for ServerFnError
impl<E: Error> From<E> for ServerFnError
Source§impl From<ServerFnError> for CapturedError
impl From<ServerFnError> for CapturedError
Source§fn from(error: ServerFnError) -> Self
fn from(error: ServerFnError) -> Self
Source§impl From<ServerFnError> for RenderError
impl From<ServerFnError> for RenderError
Source§fn from(error: ServerFnError) -> Self
fn from(error: ServerFnError) -> Self
Source§impl<T: Serialize + DeserializeOwned + Debug + 'static> FromServerFnError for ServerFnError<T>
impl<T: Serialize + DeserializeOwned + Debug + 'static> FromServerFnError for ServerFnError<T>
Source§type Encoder = JsonEncoding
type Encoder = JsonEncoding
Encodes trait for references to the error type.Source§fn from_server_fn_error(err: ServerFnErrorErr) -> Self
fn from_server_fn_error(err: ServerFnErrorErr) -> Self
ServerFnErrorErr into the application-specific custom error type.