feat(registry): print more error info

Previously, only the root error's Display was
printed. Now we print Display of the root and
every parent error's as well.
This commit is contained in:
daimond113 2025-03-02 02:55:28 +01:00
parent 308320602f
commit 86a111cf83
No known key found for this signature in database
GPG key ID: 640DC95EC1190354
3 changed files with 28 additions and 6 deletions

View file

@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Changed
- Print more error info by @daimond113
## [0.2.0] - 2025-02-22
### Added
- Support deprecating and yanking packages by @daimond113

View file

@ -1,6 +1,6 @@
use crate::{
auth::{get_token_from_req, AuthImpl, UserId},
error::ReqwestErrorExt,
error::{display_error, ReqwestErrorExt},
};
use actix_web::{dev::ServiceRequest, Error as ActixError};
use reqwest::StatusCode;
@ -46,14 +46,14 @@ impl AuthImpl for GitHubAuth {
}
Err(_) => {
tracing::error!(
"failed to get user: {}",
response.into_error().await.unwrap_err()
"failed to get user info: {}",
display_error(response.into_error().await.unwrap_err())
);
return Ok(None);
}
},
Err(e) => {
tracing::error!("failed to get user: {e}");
tracing::error!("failed to send user info request: {}", display_error(e));
return Ok(None);
}
};
@ -61,7 +61,7 @@ impl AuthImpl for GitHubAuth {
let user_id = match response.json::<UserResponse>().await {
Ok(resp) => resp.user.id,
Err(e) => {
tracing::error!("failed to get user: {e}");
tracing::error!("failed to parse user info response: {}", display_error(e));
return Ok(None);
}
};

View file

@ -1,6 +1,7 @@
use actix_web::{body::BoxBody, HttpResponse, ResponseError};
use pesde::source::git_index::errors::{ReadFile, RefreshError, TreeError};
use serde::Serialize;
use std::error::Error;
use thiserror::Error;
#[derive(Debug, Error)]
@ -66,7 +67,7 @@ impl ResponseError for RegistryError {
error: format!("archive is invalid: {e}"),
}),
e => {
tracing::error!("unhandled error: {e:?}");
tracing::error!("unhandled error: {}", display_error(e));
HttpResponse::InternalServerError().finish()
}
}
@ -87,3 +88,20 @@ impl ReqwestErrorExt for reqwest::Response {
}
}
}
pub fn display_error<E: Error>(err: E) -> String {
let mut causes = vec![];
let mut source = err.source();
while let Some(src) = source {
causes.push(format!("\t- {src}"));
source = src.source();
}
format!(
"{err}{}",
if causes.is_empty() {
"".into()
} else {
format!("\n{}", causes.join("\n"))
}
)
}