Remove unwrap from example-reverse-proxy

This commit is contained in:
David Pedersen 2023-07-01 23:08:49 +02:00
parent b1b0e45683
commit 99e8828df5

View file

@ -15,7 +15,7 @@ use axum::{
routing::get, routing::get,
Router, Router,
}; };
use hyper::client::HttpConnector; use hyper::{client::HttpConnector, StatusCode};
type Client = hyper::client::Client<HttpConnector, Body>; type Client = hyper::client::Client<HttpConnector, Body>;
@ -34,7 +34,7 @@ async fn main() {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
async fn handler(State(client): State<Client>, mut req: Request) -> Response { async fn handler(State(client): State<Client>, mut req: Request) -> Result<Response, StatusCode> {
let path = req.uri().path(); let path = req.uri().path();
let path_query = req let path_query = req
.uri() .uri()
@ -46,7 +46,11 @@ async fn handler(State(client): State<Client>, mut req: Request) -> Response {
*req.uri_mut() = Uri::try_from(uri).unwrap(); *req.uri_mut() = Uri::try_from(uri).unwrap();
client.request(req).await.unwrap().into_response() Ok(client
.request(req)
.await
.map_err(|_| StatusCode::BAD_REQUEST)?
.into_response())
} }
async fn server() { async fn server() {