store path info in the internal server error member

This commit is contained in:
yggverse 2025-07-04 16:40:47 +03:00
parent 46e781b3ad
commit 01c4eec592
3 changed files with 21 additions and 13 deletions

View file

@ -9,6 +9,7 @@ pub enum Response<'a> {
}, },
InternalServerError { InternalServerError {
error: String, error: String,
path: Option<PathBuf>,
query: Option<&'a str>, query: Option<&'a str>,
}, },
NotFound { NotFound {

View file

@ -73,11 +73,12 @@ impl Connection {
} }
} }
Err(e) => match self.response(Response::InternalServerError { Err(e) => match self.response(Response::InternalServerError {
query: None,
error: format!( error: format!(
"[{}] < [{}] failed to handle incoming request: `{e}`", "[{}] < [{}] failed to handle incoming request: `{e}`",
self.address.server, self.address.client self.address.server, self.address.client
), ),
path: None,
query: None,
}) { }) {
Ok(sent) => { Ok(sent) => {
t += sent; t += sent;
@ -134,10 +135,12 @@ impl Connection {
) )
} }
} }
Response::InternalServerError { query, error } => { Response::InternalServerError { error, path, query } => {
eprintln!( eprintln!(
"[{}] > [{}] `{query:?}`: internal server error: `{error}`", "[{}] > [{}] `{query:?}` (`{:?}`): internal server error: `{error}`",
self.address.server, self.address.client self.address.server,
self.address.client,
path.map(|p| p.to_string_lossy().to_string()),
); );
self.session.template.internal_server_error() self.session.template.internal_server_error()
} }

View file

@ -43,7 +43,7 @@ impl Public {
} }
pub fn request(&self, query: &str, mut callback: impl FnMut(Response) -> bool) -> bool { pub fn request(&self, query: &str, mut callback: impl FnMut(Response) -> bool) -> bool {
let p = { let path = {
// access restriction zone, change carefully! // access restriction zone, change carefully!
let mut path = PathBuf::from(&self.public_dir); let mut path = PathBuf::from(&self.public_dir);
path.push(query.trim_matches('/')); path.push(query.trim_matches('/'));
@ -67,20 +67,21 @@ impl Public {
} }
} }
}; };
match fs::metadata(&p) { match fs::metadata(&path) {
Ok(t) => match (t.is_dir(), t.is_file()) { Ok(t) => match (t.is_dir(), t.is_file()) {
(true, _) => callback(match self.list(&p) { (true, _) => callback(match self.list(&path) {
Ok(data) => Response::Directory { Ok(data) => Response::Directory {
query, query,
data, data,
is_root: p == self.public_dir, is_root: path == self.public_dir,
}, },
Err(e) => Response::InternalServerError { Err(e) => Response::InternalServerError {
query: Some(query),
error: e.to_string(), error: e.to_string(),
path: Some(path),
query: Some(query),
}, },
}), }),
(_, true) => match fs::File::open(p) { (_, true) => match fs::File::open(&path) {
Ok(mut f) => loop { Ok(mut f) => loop {
let mut b = vec![0; self.read_chunk]; let mut b = vec![0; self.read_chunk];
match f.read(&mut b) { match f.read(&mut b) {
@ -92,22 +93,25 @@ impl Public {
} }
Err(e) => { Err(e) => {
return callback(Response::InternalServerError { return callback(Response::InternalServerError {
query: Some(query),
error: format!("failed to read response chunk: `{e}`"), error: format!("failed to read response chunk: `{e}`"),
path: Some(path),
query: Some(query),
}); });
} }
} }
}, },
Err(e) => callback(Response::InternalServerError { Err(e) => callback(Response::InternalServerError {
query: Some(query),
error: format!("failed to read response: `{e}`"), error: format!("failed to read response: `{e}`"),
path: Some(path),
query: Some(query),
}), }),
}, },
_ => panic!(), // unexpected _ => panic!(), // unexpected
}, },
Err(e) => callback(Response::InternalServerError { Err(e) => callback(Response::InternalServerError {
query: Some(query),
error: format!("failed to read storage: `{e}`"), error: format!("failed to read storage: `{e}`"),
path: Some(path),
query: Some(query),
}), }),
} }
} }