Skip to content

Commit

Permalink
Handle hyper::http::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
hhakanisler committed Mar 22, 2024
1 parent 537d719 commit bd86195
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,15 +133,13 @@ fn get_metrics() -> Result<Response<BoxBody>> {
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<Response<BoxBody>> {
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(full(NOTFOUND))
.unwrap())
.body(full(NOTFOUND))?)
}

fn full<T: Into<Bytes>>(chunk: T) -> BoxBody {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bd86195

Please sign in to comment.