From bd861956a204545eb6de04aefe9171f68fe4210b Mon Sep 17 00:00:00 2001 From: Hakan Isler Date: Fri, 22 Mar 2024 11:04:38 +0100 Subject: [PATCH] Handle hyper::http::Error --- src/error.rs | 3 +++ src/metrics.rs | 11 ++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index e87d207..ec57ae8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -35,6 +35,9 @@ pub enum DshError { Prometheus(#[from] prometheus::Error), #[error("Convert bytes to utf8 error: {0}")] Utf8(#[from] std::string::FromUtf8Error), + #[cfg(feature = "metrics")] + #[error("Hyper error: {0}")] + HyperError(#[from] hyper::http::Error), } impl From<(&'static str, std::io::Error)> for DshError { diff --git a/src/metrics.rs b/src/metrics.rs index fa1ed79..322a3b6 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -57,11 +57,11 @@ use hyper::body::Incoming; use hyper::server::conn::http1; use hyper::service::service_fn; use hyper::{header, Method, Request, Response, StatusCode}; -pub use hyper_util::rt::TokioIo; +use hyper_util::rt::TokioIo; pub use lazy_static::lazy_static; use log::error; pub use prometheus::register_int_counter; -pub use prometheus::Encoder; +use prometheus::Encoder; pub use prometheus::IntCounter; use tokio::net::TcpListener; use tokio::task::JoinHandle; @@ -133,15 +133,13 @@ fn get_metrics() -> Result> { Ok(Response::builder() .status(StatusCode::OK) .header(header::CONTENT_TYPE, prometheus::TEXT_FORMAT) - .body(full(metrics_to_string().unwrap_or_default())) - .unwrap()) + .body(full(metrics_to_string().unwrap_or_default()))?) } fn not_found() -> Result> { Ok(Response::builder() .status(StatusCode::NOT_FOUND) - .body(full(NOTFOUND)) - .unwrap()) + .body(full(NOTFOUND))?) } fn full>(chunk: T) -> BoxBody { @@ -236,7 +234,6 @@ mod tests { // Send a request to the server let request = to_get_req(&url); - let response = request_sender.send_request(request).await.unwrap(); // Check if the server returns a 200 status