Fix error logging on file not found

This commit is contained in:
2021-03-19 10:37:18 +01:00
parent 53c72d9f90
commit ae77837ec0
2 changed files with 14 additions and 6 deletions

View File

@ -20,6 +20,7 @@ FROM scratch
WORKDIR /
CMD mkdir /scripts
CMD touch /scripts/curl.sh
CMD echo "#!/bin/sh" >> /scripts/curl.sh
CMD echo "echo Hello there!" >> /scripts/curl.sh

View File

@ -1,8 +1,7 @@
use compound_error::CompoundError;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Client, Request, Response, Server, Uri};
use kv_log_macro::info;
use log::error;
use kv_log_macro::{error, info};
use std::net::SocketAddr;
use std::path::PathBuf;
use structopt::StructOpt;
@ -43,8 +42,16 @@ async fn proxy_pass(mut req: Request<Body>, opt: &Opt) -> Result<Response<Body>,
let mut error = None;
let response = if user_agent_matches {
let file = fs::read_to_string(&opt.file).await?;
Response::new(file.into())
match fs::read_to_string(&opt.file).await {
Ok(file) => Response::new(file.into()),
Err(e) => {
error = Some(format!("{:?}", e));
Response::builder()
.status(404)
.body("File not found".into())
.expect("infallible response")
}
}
} else {
let proxy_uri: Uri = opt.proxy_pass.parse().expect("proxy uri");
@ -62,11 +69,11 @@ async fn proxy_pass(mut req: Request<Body>, opt: &Opt) -> Result<Response<Body>,
match client.request(req).await {
Ok(response) => response,
Err(e) => {
error = Some(e);
error = Some(format!("{:?}", e));
Response::builder()
.status(503)
.body("503 Service Unavailable".into())
.unwrap()
.expect("infallible response")
}
}
};