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
This commit is contained in:
+5
-9
@@ -575,18 +575,14 @@ fn json_response(status: u16, body: &str) -> Response<Full<Bytes>> {
|
||||
}
|
||||
|
||||
fn text_response(status: u16, content_type: &str, body: String) -> Response<Full<Bytes>> {
|
||||
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<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||
|
||||
Reference in New Issue
Block a user