chore: add more error detial

This commit is contained in:
xfy
2025-07-01 21:38:23 +08:00
parent 36cd153fab
commit f96e440264
2 changed files with 31 additions and 10 deletions

View File

@ -157,21 +157,30 @@ pub async fn serve(
let scheme = req.uri().scheme_str().unwrap_or("http");
let port = parse_port_from_host(&host, scheme).ok_or(RouteError::BadRequest())?;
let route_map = &HOSTS.get(&port).ok_or(RouteError::BadRequest())?.route_map;
let route_map = &HOSTS
.get(&port)
.ok_or(RouteError::BadRequest())
.with_context(|| {
format!("Hosts not found for port: {port}, host: {host}, scheme: {scheme}")
})?
.route_map;
tracing::debug!("Route map entries: {:?}", route_map);
let parent_path = resolve_parent_path(&req_uri, path.as_ref());
tracing::debug!("parent path: {:?}", parent_path);
let proxy_config = route_map
.get(&parent_path)
.ok_or(RouteError::RouteNotFound())?;
.ok_or(RouteError::RouteNotFound())
.with_context(|| format!("route not found: {parent_path}"))?;
tracing::debug!("proxy pass: {:?}", proxy_config);
let Some(ref proxy_pass) = proxy_config.proxy_pass else {
return handle_custom_page(proxy_config, req, true).await;
};
let uri = format!("{proxy_pass}{path_query}");
tracing::debug!("reverse proxy uri: {:?}", &uri);
*req.uri_mut() = Uri::try_from(uri.clone()).map_err(|_| RouteError::InternalError())?;
*req.uri_mut() = Uri::try_from(uri.clone())
.map_err(|_| RouteError::InternalError())
.with_context(|| format!("uri not found: {uri}"))?;
let timeout = proxy_config.proxy_timeout;
@ -210,7 +219,8 @@ pub async fn serve(
reqwest_response.headers(),
response_builder
.headers_mut()
.ok_or(RouteError::InternalError())?,
.ok_or(RouteError::InternalError())
.with_context(|| "headers not found")?,
);
let res = response_builder
.body(Body::from_stream(reqwest_response.bytes_stream()))

View File

@ -52,23 +52,27 @@ async fn custom_page(
host_route
.error_page
.as_ref()
.ok_or(RouteError::RouteNotFound())?
.ok_or(RouteError::RouteNotFound())
.with_context(|| "error page not found")?
} else {
host_route
.not_found_page
.as_ref()
.ok_or(RouteError::RouteNotFound())?
.ok_or(RouteError::RouteNotFound())
.with_context(|| "not found page not found")?
};
let root = host_route
.root
.as_ref()
.ok_or(RouteError::InternalError())?;
.ok_or(RouteError::InternalError())
.with_context(|| "root not found")?;
let path = format!("{}/{}", root, page.page);
let status = StatusCode::from_str(page.status.to_string().as_ref())
.map_err(|_| RouteError::BadRequest())?;
.map_err(|_| RouteError::BadRequest())
.with_context(|| format!("status code not found: {}", page.status))?;
tracing::debug!("custom not found path: {:?}", path);
@ -125,11 +129,18 @@ pub async fn serve(
// which is `host_route.location`
let scheme = request.uri().scheme_str().unwrap_or("http");
let port = parse_port_from_host(&host, scheme).ok_or(RouteError::BadRequest())?;
let route_map = &HOSTS.get(&port).ok_or(RouteError::BadRequest())?.route_map;
let route_map = &HOSTS
.get(&port)
.ok_or(RouteError::BadRequest())
.with_context(|| {
format!("Hosts not found for port: {port}, host: {host}, scheme: {scheme}")
})?
.route_map;
debug!("Route map entries: {:?}", route_map);
let host_route = route_map
.get(&parent_path)
.ok_or(RouteError::RouteNotFound())?;
.ok_or(RouteError::RouteNotFound())
.with_context(|| format!("route not found: {parent_path}"))?;
debug!("route: {:?}", host_route);
// after route found
// check static file root configuration