From da311c657e4586212279aa08785f08e168dae295 Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Tue, 30 Dec 2025 22:24:58 +0100 Subject: [PATCH 1/6] refactor: avoid unwrap in service_struct_impl example --- examples/service_struct_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/service_struct_impl.rs b/examples/service_struct_impl.rs index d0be1eb708..67d88988f8 100644 --- a/examples/service_struct_impl.rs +++ b/examples/service_struct_impl.rs @@ -51,7 +51,7 @@ impl Service> for Svc { fn call(&self, req: Request) -> Self::Future { fn mk_response(s: String) -> Result>, hyper::Error> { - Ok(Response::builder().body(Full::new(Bytes::from(s))).unwrap()) + Ok(Response::new(Full::new(Bytes::from(s)))) } if req.uri().path() != "/favicon.ico" { From 9b3f49a155e5ce3faf53061341a76bc4046b37ef Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Tue, 30 Dec 2025 22:30:41 +0100 Subject: [PATCH 2/6] refactor: avoid unwrap in send_file example --- examples/send_file.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/send_file.rs b/examples/send_file.rs index b2ef328a1f..56f206b4d8 100644 --- a/examples/send_file.rs +++ b/examples/send_file.rs @@ -61,7 +61,7 @@ fn not_found() -> Response> { Response::builder() .status(StatusCode::NOT_FOUND) .body(Full::new(NOTFOUND.into()).map_err(|e| match e {}).boxed()) - .unwrap() + .expect("not found should be valid") } async fn simple_file_send(filename: &str) -> Result>> { @@ -85,7 +85,7 @@ async fn simple_file_send(filename: &str) -> Result Date: Tue, 30 Dec 2025 22:36:57 +0100 Subject: [PATCH 3/6] refactor: avoid unwrap in web_api example --- examples/web_api.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/web_api.rs b/examples/web_api.rs index ffc62ba89b..136c8556c8 100644 --- a/examples/web_api.rs +++ b/examples/web_api.rs @@ -29,7 +29,7 @@ async fn client_request_response() -> Result> { .uri(URL) .header(header::CONTENT_TYPE, "application/json") .body(Full::new(Bytes::from(POST_DATA))) - .unwrap(); + .expect("post data should be valid"); let host = req.uri().host().expect("uri has no host"); let port = req.uri().port_u16().expect("uri has no port"); @@ -73,11 +73,11 @@ async fn api_get_response() -> Result> { Ok(json) => Response::builder() .header(header::CONTENT_TYPE, "application/json") .body(full(json)) - .unwrap(), + .expect("json response should be valid"), Err(_) => Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR) .body(full(INTERNAL_SERVER_ERROR)) - .unwrap(), + .expect("internal server error response should be valid"), }; Ok(res) } @@ -93,7 +93,7 @@ async fn response_examples(req: Request) -> Result Date: Tue, 30 Dec 2025 22:38:47 +0100 Subject: [PATCH 4/6] refactor: avoid unwrap in upgrades example --- examples/upgrades.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/upgrades.rs b/examples/upgrades.rs index b94a8b9c42..26bc708606 100644 --- a/examples/upgrades.rs +++ b/examples/upgrades.rs @@ -100,7 +100,7 @@ async fn client_upgrade_request(addr: SocketAddr) -> Result<()> { .uri(format!("http://{}/", addr)) .header(UPGRADE, "foobar") .body(Empty::::new()) - .unwrap(); + .expect("request should be valid"); let stream = TcpStream::connect(addr).await?; let io = TokioIo::new(stream); From 3c1626cc99d86e2b8e3cb9da9ac54a9f448e9e6f Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Tue, 30 Dec 2025 22:50:50 +0100 Subject: [PATCH 5/6] refactor: avoid unwrap in params example --- examples/params.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/params.rs b/examples/params.rs index 8774046a67..5fe5fc635b 100644 --- a/examples/params.rs +++ b/examples/params.rs @@ -52,7 +52,7 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .unwrap()); + .expect("missing should be valid")); }; let number = if let Some(n) = params.get("number") { if let Ok(v) = n.parse::() { @@ -61,13 +61,13 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(NOTNUMERIC)) - .unwrap()); + .expect("notnumeric should be valid")); } } else { return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .unwrap()); + .expect("missing should be valid")); }; // Render the response. This will often involve @@ -86,7 +86,7 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .unwrap()); + .expect("missing should be valid")); }; let params = form_urlencoded::parse(query.as_bytes()) .into_owned() @@ -97,7 +97,7 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .unwrap()); + .expect("missing should be valid")); }; let body = format!("You requested {}", page); Ok(Response::new(full(body))) @@ -105,7 +105,7 @@ async fn param_example( _ => Ok(Response::builder() .status(StatusCode::NOT_FOUND) .body(empty()) - .unwrap()), + .expect("not found should be valid")), } } From d640e4c0c3aefce88ad6822b2a80329701cd0f12 Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Wed, 7 Jan 2026 22:18:34 +0100 Subject: [PATCH 6/6] fix: better error messages --- examples/params.rs | 12 ++++++------ examples/send_file.rs | 4 ++-- examples/upgrades.rs | 2 +- examples/web_api.rs | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/params.rs b/examples/params.rs index 5fe5fc635b..0f26efb8bb 100644 --- a/examples/params.rs +++ b/examples/params.rs @@ -52,7 +52,7 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .expect("missing should be valid")); + .expect("constant status won't error")); }; let number = if let Some(n) = params.get("number") { if let Ok(v) = n.parse::() { @@ -61,13 +61,13 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(NOTNUMERIC)) - .expect("notnumeric should be valid")); + .expect("constant status won't error")); } } else { return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .expect("missing should be valid")); + .expect("constant status won't error")); }; // Render the response. This will often involve @@ -86,7 +86,7 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .expect("missing should be valid")); + .expect("constant status won't error")); }; let params = form_urlencoded::parse(query.as_bytes()) .into_owned() @@ -97,7 +97,7 @@ async fn param_example( return Ok(Response::builder() .status(StatusCode::UNPROCESSABLE_ENTITY) .body(full(MISSING)) - .expect("missing should be valid")); + .expect("constant status won't error")); }; let body = format!("You requested {}", page); Ok(Response::new(full(body))) @@ -105,7 +105,7 @@ async fn param_example( _ => Ok(Response::builder() .status(StatusCode::NOT_FOUND) .body(empty()) - .expect("not found should be valid")), + .expect("constant status won't error")), } } diff --git a/examples/send_file.rs b/examples/send_file.rs index 56f206b4d8..d3dc115a5d 100644 --- a/examples/send_file.rs +++ b/examples/send_file.rs @@ -61,7 +61,7 @@ fn not_found() -> Response> { Response::builder() .status(StatusCode::NOT_FOUND) .body(Full::new(NOTFOUND.into()).map_err(|e| match e {}).boxed()) - .expect("not found should be valid") + .expect("constant status won't error") } async fn simple_file_send(filename: &str) -> Result>> { @@ -85,7 +85,7 @@ async fn simple_file_send(filename: &str) -> Result Result<()> { .uri(format!("http://{}/", addr)) .header(UPGRADE, "foobar") .body(Empty::::new()) - .expect("request should be valid"); + .expect("uri/header parse won't error"); let stream = TcpStream::connect(addr).await?; let io = TokioIo::new(stream); diff --git a/examples/web_api.rs b/examples/web_api.rs index 136c8556c8..ff2ea90b3d 100644 --- a/examples/web_api.rs +++ b/examples/web_api.rs @@ -29,7 +29,7 @@ async fn client_request_response() -> Result> { .uri(URL) .header(header::CONTENT_TYPE, "application/json") .body(Full::new(Bytes::from(POST_DATA))) - .expect("post data should be valid"); + .expect("uri/header parse from constants won't error"); let host = req.uri().host().expect("uri has no host"); let port = req.uri().port_u16().expect("uri has no port"); @@ -73,11 +73,11 @@ async fn api_get_response() -> Result> { Ok(json) => Response::builder() .header(header::CONTENT_TYPE, "application/json") .body(full(json)) - .expect("json response should be valid"), + .expect("header parse from constant won't error"), Err(_) => Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR) .body(full(INTERNAL_SERVER_ERROR)) - .expect("internal server error response should be valid"), + .expect("constant status won't error"), }; Ok(res) } @@ -93,7 +93,7 @@ async fn response_examples(req: Request) -> Result