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

View File

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