Proper http error response codes
This commit is contained in:
parent
bf7eb1a411
commit
f29752db77
23
src/main.rs
23
src/main.rs
@ -3,6 +3,7 @@ use axum::{
|
||||
State,
|
||||
Path,
|
||||
},
|
||||
http::StatusCode,
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
@ -142,9 +143,9 @@ async fn route_index(
|
||||
async fn route_station_overview(
|
||||
State(state): State<AppState>,
|
||||
Path(station): Path<String>
|
||||
) -> Result<Html<String>, String> {
|
||||
) -> Result<Html<String>, (StatusCode, String)> {
|
||||
if !state.stations.contains_key(&station) {
|
||||
return Err(String::from(""));
|
||||
return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station")));
|
||||
}
|
||||
|
||||
let mut out = String::new();
|
||||
@ -163,40 +164,40 @@ async fn route_station_overview(
|
||||
async fn route_station_to_dest_station(
|
||||
State(state): State<AppState>,
|
||||
Path((station_id, dest_station_id)): Path<(String, String)>
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, (StatusCode, String)> {
|
||||
if !state.stations.contains_key(&station_id) {
|
||||
return Err(String::from(""));
|
||||
return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station")));
|
||||
}
|
||||
|
||||
if !state.stations.contains_key(&dest_station_id) {
|
||||
return Err(String::from(""));
|
||||
return Err((StatusCode::NOT_FOUND, String::from("Unknown destination station")));
|
||||
}
|
||||
|
||||
let station_properties = match state.stations.get(&station_id) {
|
||||
Some(v) => v,
|
||||
None => return Err(String::from("")),
|
||||
None => return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station properties for departing station missing"))),
|
||||
};
|
||||
|
||||
let dest_station_properties = match state.stations.get(&dest_station_id) {
|
||||
Some(v) => v,
|
||||
None => return Err(String::from("")),
|
||||
None => return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station properties for destination station missing"))),
|
||||
};
|
||||
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let stationdata_req = client.get(format!("https://bahn.expert/api/iris/v2/abfahrten/{}?lookahead=150&lookbehind=10", station_properties.code))
|
||||
.send().await
|
||||
.map_err(|_err| String::from("Station Data cannot be fetched"))?;
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be fetched")))?;
|
||||
|
||||
if stationdata_req.status() != reqwest::StatusCode::OK {
|
||||
return Err(String::from("Station Data returned an unexpected response"));
|
||||
return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data returned an unexpected response")));
|
||||
}
|
||||
|
||||
let stationdata_body = stationdata_req.text().await
|
||||
.map_err(|_err| "Station Data cannot be read")?;
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be read")))?;
|
||||
|
||||
let stationdata: Departures = serde_json::from_str(stationdata_body.as_str())
|
||||
.map_err(|err| format!("{}", err))?;
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", err)))?;
|
||||
|
||||
let mut out = String::new();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user