From 0be0ce4c1563e87b04d9c814efc9070276744b40 Mon Sep 17 00:00:00 2001 From: Valerio Ageno Date: Sat, 15 Jun 2024 14:04:08 +0200 Subject: [PATCH] feat: handle 404 on tutorial --- examples/tutorial/src/routes/index.rs | 2 +- examples/tutorial/src/routes/pokemons/[pokemon].rs | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/tutorial/src/routes/index.rs b/examples/tutorial/src/routes/index.rs index 14a47f6e..1c2ed45a 100644 --- a/examples/tutorial/src/routes/index.rs +++ b/examples/tutorial/src/routes/index.rs @@ -24,7 +24,7 @@ async fn get_all_pokemons(_req: Request<'_>, fetch: reqwest::Client) -> Response Response::Props(Props::new(data)) } Err(_err) => Response::Props(Props::new_with_status( - Pokemons { results: vec![] }, + "{}", // Return empty JSON StatusCode::INTERNAL_SERVER_ERROR, )), }; diff --git a/examples/tutorial/src/routes/pokemons/[pokemon].rs b/examples/tutorial/src/routes/pokemons/[pokemon].rs index 7be62583..7b6275d7 100644 --- a/examples/tutorial/src/routes/pokemons/[pokemon].rs +++ b/examples/tutorial/src/routes/pokemons/[pokemon].rs @@ -19,16 +19,15 @@ async fn get_pokemon(req: Request<'_>, fetch: reqwest::Client) -> Response { return match fetch.get(format!("{POKEMON_API}/{pokemon}")).send().await { Ok(res) => { + if res.status() == StatusCode::NOT_FOUND { + return Response::Props(Props::new_with_status("{}", StatusCode::NOT_FOUND)); + } + let data = res.json::().await.unwrap(); Response::Props(Props::new(data)) } Err(_err) => Response::Props(Props::new_with_status( - Pokemon { - name: "Nope".to_string(), - id: 0, - weight: 0, - height: 0, - }, + "{}", StatusCode::INTERNAL_SERVER_ERROR, )), };