From 6f40921576a9ac1c1db7aed8dab468c35d433037 Mon Sep 17 00:00:00 2001 From: zhenyi <434836402@qq.com> Date: Fri, 12 Jun 2026 15:10:00 +0800 Subject: [PATCH] refactor(metrics): simplify response building with unwrap_or_else - Replace explicit match statement with unwrap_or_else for cleaner error handling - Maintain same error logging behavior when response building fails - Reduce code complexity and improve readability - Keep identical fallback response creation on builder errors --- metrics.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/metrics.rs b/metrics.rs index b33ad02..d3b2c2f 100644 --- a/metrics.rs +++ b/metrics.rs @@ -575,18 +575,14 @@ fn json_response(status: u16, body: &str) -> Response> { } fn text_response(status: u16, content_type: &str, body: String) -> Response> { - match Response::builder() + Response::builder() .status(status) .header("Content-Type", content_type) .header("Connection", "close") - .body(Full::new(Bytes::from(body))) - { - Ok(response) => response, - Err(err) => { - tracing::error!(error = %err, "failed to build text response"); - Response::new(Full::new(Bytes::from_static(b"response build failed"))) - } - } + .body(Full::new(Bytes::from(body))).unwrap_or_else(|err| { + tracing::error!(error = %err, "failed to build text response"); + Response::new(Full::new(Bytes::from_static(b"response build failed"))) + }) } async fn handle_request(req: Request) -> Result>, Infallible> {