From 86a111cf83ddc5abee06fb5e3996e99dce80d7ac Mon Sep 17 00:00:00 2001 From: daimond113 Date: Sun, 2 Mar 2025 02:55:28 +0100 Subject: [PATCH] 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. --- registry/CHANGELOG.md | 4 ++++ registry/src/auth/github.rs | 10 +++++----- registry/src/error.rs | 20 +++++++++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/registry/CHANGELOG.md b/registry/CHANGELOG.md index 0700508..98d4aab 100644 --- a/registry/CHANGELOG.md +++ b/registry/CHANGELOG.md @@ -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 diff --git a/registry/src/auth/github.rs b/registry/src/auth/github.rs index 4ff5d46..802a741 100644 --- a/registry/src/auth/github.rs +++ b/registry/src/auth/github.rs @@ -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::().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); } }; diff --git a/registry/src/error.rs b/registry/src/error.rs index c7feb38..9c22712 100644 --- a/registry/src/error.rs +++ b/registry/src/error.rs @@ -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(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")) + } + ) +}