diff --git a/src/util/error.rs b/src/util/error.rs index fc0aa54..805cfa4 100644 --- a/src/util/error.rs +++ b/src/util/error.rs @@ -220,6 +220,27 @@ impl fmt::Display for Error { } } +impl From 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 { /// Add context to the `Result` if it is an `Err`. #[track_caller]