Compare commits

...

4 Commits

Author SHA1 Message Date
3640039168 Add required conversions for returning Error from server functions
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 5s
Push Workflows / test (push) Successful in 18s
Push Workflows / clippy (push) Successful in 14s
Push Workflows / docs (push) Successful in 18s
Push Workflows / build (push) Successful in 42s
Push Workflows / nix-build (push) Successful in 4m50s
2026-06-21 13:55:40 -04:00
9fd716c752 Add ServerFnError error variant 2026-06-21 13:51:56 -04:00
f159e12400 Contextualize error into result instead of error 2026-06-21 13:51:12 -04:00
d16a66a2f5 Use match instead of map_err for Contextualize on Result 2026-06-21 13:48:41 -04:00

View File

@@ -220,6 +220,27 @@ impl fmt::Display for Error {
} }
} }
impl From<ServerFnError> for Error {
#[track_caller]
fn from(err: ServerFnError) -> Error {
Error::new_here(ErrorType::ServerFnError(err))
}
}
#[cfg(feature = "server")]
impl dioxus_fullstack::AsStatusCode for Error {
fn as_status_code(&self) -> StatusCode {
match &self.source {
ErrorType::Database(msg) if *msg == (diesel::result::Error::NotFound).to_string() => {
StatusCode::NOT_FOUND
}
ErrorType::Database(_) => StatusCode::INTERNAL_SERVER_ERROR,
ErrorType::Error(_) => StatusCode::INTERNAL_SERVER_ERROR,
ErrorType::ServerFnError(e) => e.as_status_code(),
}
}
}
pub trait Contextualize<R> { pub trait Contextualize<R> {
/// Add context to the `Result` if it is an `Err`. /// Add context to the `Result` if it is an `Err`.
#[track_caller] #[track_caller]
@@ -229,7 +250,12 @@ pub trait Contextualize<R> {
impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for Result<T, E> { impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for Result<T, E> {
#[track_caller] #[track_caller]
fn err_context(self, context: impl Into<String>) -> Result<T, Error> { fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
self.map_err(|e| e.into().with_context(context)) // Closures can't (currently) `track_caller`, so a simple map_err doesn't work
// See https://github.com/rust-lang/rust/issues/87417
match self {
Ok(e) => Ok(e),
Err(e) => Err(e.into().with_context(context)),
}
} }
} }
@@ -240,10 +266,10 @@ impl<T> Contextualize<Result<T, Error>> for Option<T> {
} }
} }
impl<E: Into<Error>> Contextualize<Error> for E { impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for E {
#[track_caller] #[track_caller]
fn err_context(self, context: impl Into<String>) -> Error { fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
self.into().with_context(context) Err(self.into().with_context(context))
} }
} }
@@ -256,6 +282,9 @@ pub enum ErrorType {
#[error("{0}")] #[error("{0}")]
Error(String), Error(String),
#[error("Server function error: {0}")]
ServerFnError(ServerFnError),
} }
impl From<ErrorType> for Error { impl From<ErrorType> for Error {