From 5c65bf7706d68c592ab046a1cdb4fa53aec3ca38 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 21 Nov 2026 21:57:24 -0600 Subject: [PATCH] refactor(web-gen-api-axum): extract error template crate --- pkgs/start-axum-error/Cargo.toml | 16 +++++ pkgs/start-axum-error/src/lib.rs | 76 ++++++++++++++++++++ pkgs/web-gen-api-axum/Cargo.lock | 14 +++- pkgs/web-gen-api-axum/Cargo.toml | 5 +- pkgs/web-gen-api-axum/src/error_template.rs | 77 +-------------------- 5 files changed, 110 insertions(+), 78 deletions(-) create mode 100644 pkgs/start-axum-error/Cargo.toml create mode 100644 pkgs/start-axum-error/src/lib.rs diff --git a/pkgs/start-axum-error/Cargo.toml b/pkgs/start-axum-error/Cargo.toml new file mode 100644 index 0000000000..d550ffcdcf --- /dev/null +++ b/pkgs/start-axum-error/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "start-axum-error" +version = "0.1.0" +edition = "2021" + +[dependencies] +cfg-if = "1" +http = "0.2.8" +leptos = "=0.4.10" +leptos_axum = { version = "=0.4.10", optional = true } +thiserror = "1.0.38" +tracing = { version = "0.1.40", optional = true } + +[features] +hydrate = ["dep:tracing", "leptos/hydrate"] +ssr = ["dep:leptos_axum", "dep:tracing", "leptos/ssr"] diff --git a/pkgs/start-axum-error/src/lib.rs b/pkgs/start-axum-error/src/lib.rs new file mode 100644 index 0000000000..3a947fa59b --- /dev/null +++ b/pkgs/start-axum-error/src/lib.rs @@ -0,0 +1,76 @@ +use cfg_if::cfg_if; +use http::status::StatusCode; +use leptos::*; +use thiserror::Error; + +#[cfg(feature = "ssr")] +use leptos_axum::ResponseOptions; + +#[derive(Clone, Debug, Error)] +pub enum AppError { + #[error("Not Found")] + NotFound, +} + +impl AppError { + pub fn status_code(&self) -> StatusCode { + match self { + AppError::NotFound => StatusCode::NOT_FOUND, + } + } +} + +// A basic function to display errors served by the error boundaries. +// Feel free to do more complicated things here than just displaying the error. +#[component] +pub fn ErrorTemplate( + cx: Scope, + #[prop(optional)] outside_errors: Option, + #[prop(optional)] errors: Option>, +) -> impl IntoView { + let errors = match outside_errors { + Some(e) => create_rw_signal(cx, e), + None => match errors { + Some(e) => e, + None => panic!("No Errors found and we expected errors!"), + }, + }; + // Get Errors from Signal + let errors = errors.get(); + + // Downcast lets us take a type that implements `std::error::Error` + let errors: Vec = errors + .into_iter() + .filter_map(|(_k, v)| v.downcast_ref::().cloned()) + .collect(); + println!("Errors: {errors:#?}"); + + // Only the response code for the first error is actually sent from the server + // this may be customized by the specific application + cfg_if! { if #[cfg(feature="ssr")] { + let response = use_context::(cx); + if let Some(response) = response { + response.set_status(errors[0].status_code()); + } + }} + + view! {cx, +

{if errors.len() > 1 {"Errors"} else {"Error"}}

+ {error_code.to_string()} +

"Error: " {error_string}

+ } + } + /> + } +} diff --git a/pkgs/web-gen-api-axum/Cargo.lock b/pkgs/web-gen-api-axum/Cargo.lock index b52e7b231c..f256bbcfe5 100644 --- a/pkgs/web-gen-api-axum/Cargo.lock +++ b/pkgs/web-gen-api-axum/Cargo.lock @@ -2756,7 +2756,7 @@ dependencies = [ "once_cell", "serde", "simple_logger", - "thiserror", + "start-axum-error", "tokio", "tower", "tower-http", @@ -2765,6 +2765,18 @@ dependencies = [ "web-sys", ] +[[package]] +name = "start-axum-error" +version = "0.1.0" +dependencies = [ + "cfg-if", + "http", + "leptos", + "leptos_axum", + "thiserror", + "tracing", +] + [[package]] name = "strsim" version = "0.10.0" diff --git a/pkgs/web-gen-api-axum/Cargo.toml b/pkgs/web-gen-api-axum/Cargo.toml index a3a4bf0b5c..1380e52b27 100755 --- a/pkgs/web-gen-api-axum/Cargo.toml +++ b/pkgs/web-gen-api-axum/Cargo.toml @@ -15,13 +15,13 @@ leptos = "=0.4.10" leptos_axum = { version = "=0.4.10", optional = true } leptos_meta = "=0.4.10" leptos_router = "=0.4.10" +start-axum-error = { path = "../start-axum-error" } log = "0.4" simple_logger = "4" tokio = { version = "1.38.2", optional = true } tower = { version = "0.4.13", optional = true } tower-http = { version = "0.4", features = ["fs"], optional = true } wasm-bindgen = "0.2.87" -thiserror = "1.0.38" tracing = { version = "0.1.40", optional = true } http = "0.2.8" serde = { version = "1.0.176", features = ["derive"] } @@ -33,12 +33,15 @@ leptos_query = "0.2.3" [features] hydrate = [ + "start-axum-error/hydrate", + "dep:tracing", "leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate", "leptos_query/hydrate", ] ssr = [ + "start-axum-error/ssr", "dep:axum", "dep:tokio", "dep:tower", diff --git a/pkgs/web-gen-api-axum/src/error_template.rs b/pkgs/web-gen-api-axum/src/error_template.rs index 3a947fa59b..018e71f741 100755 --- a/pkgs/web-gen-api-axum/src/error_template.rs +++ b/pkgs/web-gen-api-axum/src/error_template.rs @@ -1,76 +1 @@ -use cfg_if::cfg_if; -use http::status::StatusCode; -use leptos::*; -use thiserror::Error; - -#[cfg(feature = "ssr")] -use leptos_axum::ResponseOptions; - -#[derive(Clone, Debug, Error)] -pub enum AppError { - #[error("Not Found")] - NotFound, -} - -impl AppError { - pub fn status_code(&self) -> StatusCode { - match self { - AppError::NotFound => StatusCode::NOT_FOUND, - } - } -} - -// A basic function to display errors served by the error boundaries. -// Feel free to do more complicated things here than just displaying the error. -#[component] -pub fn ErrorTemplate( - cx: Scope, - #[prop(optional)] outside_errors: Option, - #[prop(optional)] errors: Option>, -) -> impl IntoView { - let errors = match outside_errors { - Some(e) => create_rw_signal(cx, e), - None => match errors { - Some(e) => e, - None => panic!("No Errors found and we expected errors!"), - }, - }; - // Get Errors from Signal - let errors = errors.get(); - - // Downcast lets us take a type that implements `std::error::Error` - let errors: Vec = errors - .into_iter() - .filter_map(|(_k, v)| v.downcast_ref::().cloned()) - .collect(); - println!("Errors: {errors:#?}"); - - // Only the response code for the first error is actually sent from the server - // this may be customized by the specific application - cfg_if! { if #[cfg(feature="ssr")] { - let response = use_context::(cx); - if let Some(response) = response { - response.set_status(errors[0].status_code()); - } - }} - - view! {cx, -

{if errors.len() > 1 {"Errors"} else {"Error"}}

- {error_code.to_string()} -

"Error: " {error_string}

- } - } - /> - } -} +pub use start_axum_error::{AppError, ErrorTemplate};