Skip to content

Commit

Permalink
make fetch poke return any error
Browse files Browse the repository at this point in the history
  • Loading branch information
m4salah committed Dec 9, 2023
1 parent 89fcc15 commit 57b109f
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/handlers/day8.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error;

use axum::{extract::Path, http::StatusCode, routing::get};
use serde::Deserialize;

Expand All @@ -13,31 +15,29 @@ impl PokeWeight {
}
}

async fn fetch_poke(poke_id: u32) -> Result<PokeWeight, StatusCode> {
async fn fetch_poke(poke_id: u32) -> Result<PokeWeight, Box<dyn Error>> {
Ok(
reqwest::get(format!("https://pokeapi.co/api/v2/pokemon/{poke_id}"))
.await
.map_err(|e| {
eprintln!("ERR: coudn't call the pokeapi {e}");
StatusCode::INTERNAL_SERVER_ERROR
})?
.await?
.json::<PokeWeight>()
.await
.map_err(|e| {
eprintln!("ERR: coudn't parse json {e}");
StatusCode::INTERNAL_SERVER_ERROR
})?,
.await?,
)
}

async fn poke_weight(Path(pokedex): Path<u32>) -> Result<String, StatusCode> {
let poke_weight = fetch_poke(pokedex).await?;
let poke_weight = fetch_poke(pokedex).await.map_err(|e| {
eprintln!("ERR: error while fetch poke {e}");
StatusCode::INTERNAL_SERVER_ERROR
})?;

Ok(format!("{}", poke_weight.extract_weight_kg()))
}

async fn poke_drop(Path(pokedex): Path<u32>) -> Result<String, StatusCode> {
let poke_weight = fetch_poke(pokedex).await?;
let poke_weight = fetch_poke(pokedex).await.map_err(|e| {
eprintln!("ERR: error while fetch poke {e}");
StatusCode::INTERNAL_SERVER_ERROR
})?;

Ok(format!(
"{}",
Expand Down

0 comments on commit 57b109f

Please sign in to comment.