From 26c0c2488dc6bdd3ebf747cf6f106dbf3a18dfda Mon Sep 17 00:00:00 2001 From: Valerio Ageno Date: Sat, 15 Jun 2024 12:30:20 +0200 Subject: [PATCH] feat: handle status codes --- crates/tuono_lib/src/response.rs | 17 ++++++++++++----- examples/tutorial/src/routes/index.rs | 6 +++++- .../tutorial/src/routes/pokemons/[pokemon].rs | 16 ++++++++++------ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/crates/tuono_lib/src/response.rs b/crates/tuono_lib/src/response.rs index 42e504d1..f217b55f 100644 --- a/crates/tuono_lib/src/response.rs +++ b/crates/tuono_lib/src/response.rs @@ -22,17 +22,24 @@ impl Props { http_code: StatusCode::OK, } } + + pub fn new_with_status(data: impl Serialize + 'static, http_code: StatusCode) -> Self { + Props { + data: Box::new(data), + http_code, + } + } } impl Response { pub fn render_to_string(&self, req: Request) -> impl IntoResponse { match self { - Self::Props(Props { data, http_code: _ }) => { + Self::Props(Props { data, http_code }) => { let payload = Payload::new(&req, data).client_payload().unwrap(); match Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload))) { - Ok(html) => Html(html), - Err(_) => Html("500 Internal server error".to_string()), + Ok(html) => (*http_code, Html(html)), + Err(_) => (*http_code, Html("500 Internal server error".to_string())), } } // TODO: Handle here other enum arms @@ -42,8 +49,8 @@ impl Response { pub fn json(&self) -> impl IntoResponse { match self { - Self::Props(Props { data, http_code: _ }) => Json(data).into_response(), - _ => axum::Json("").into_response(), + Self::Props(Props { data, http_code }) => (*http_code, Json(data)).into_response(), + _ => (StatusCode::INTERNAL_SERVER_ERROR, axum::Json("{}")).into_response(), } } } diff --git a/examples/tutorial/src/routes/index.rs b/examples/tutorial/src/routes/index.rs index f084cc8d..14a47f6e 100644 --- a/examples/tutorial/src/routes/index.rs +++ b/examples/tutorial/src/routes/index.rs @@ -1,3 +1,4 @@ +use reqwest::StatusCode; // src/routes/index.rs use serde::{Deserialize, Serialize}; use tuono_lib::{Props, Request, Response}; @@ -22,6 +23,9 @@ async fn get_all_pokemons(_req: Request<'_>, fetch: reqwest::Client) -> Response let data = res.json::().await.unwrap(); Response::Props(Props::new(data)) } - Err(_err) => Response::Props(Props::new(Pokemons { results: vec![] })), + Err(_err) => Response::Props(Props::new_with_status( + Pokemons { results: vec![] }, + StatusCode::INTERNAL_SERVER_ERROR, + )), }; } diff --git a/examples/tutorial/src/routes/pokemons/[pokemon].rs b/examples/tutorial/src/routes/pokemons/[pokemon].rs index 64244c29..7be62583 100644 --- a/examples/tutorial/src/routes/pokemons/[pokemon].rs +++ b/examples/tutorial/src/routes/pokemons/[pokemon].rs @@ -1,3 +1,4 @@ +use reqwest::StatusCode; use serde::{Deserialize, Serialize}; use tuono_lib::{Props, Request, Response}; @@ -21,11 +22,14 @@ async fn get_pokemon(req: Request<'_>, fetch: reqwest::Client) -> Response { let data = res.json::().await.unwrap(); Response::Props(Props::new(data)) } - Err(_err) => Response::Props(Props::new(Pokemon { - name: "Nope".to_string(), - id: 0, - weight: 0, - height: 0, - })), + Err(_err) => Response::Props(Props::new_with_status( + Pokemon { + name: "Nope".to_string(), + id: 0, + weight: 0, + height: 0, + }, + StatusCode::INTERNAL_SERVER_ERROR, + )), }; }