From 7a1f3c52d5dc5527f2de4a7940ad9a14c1ca84eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B6=BE=E7=80=AC=E6=A1=83=E6=A1=83?= Date: Tue, 10 Nov 2026 02:04:09 +0800 Subject: [PATCH] fix(io): print an error cause that repeats its wrapper once A wrapper error that displays its inner error and also returns it as its source made the chain print that text twice, as in "restore protected library: restore protected library: ...". A message's English now skips a link that only repeats the one before it. --- senbei-io/src/message.rs | 50 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/senbei-io/src/message.rs b/senbei-io/src/message.rs index 980ed82..edf05fd 100644 --- a/senbei-io/src/message.rs +++ b/senbei-io/src/message.rs @@ -1,8 +1,9 @@ //! Translatable messages for errors that reach the I/O layer. //! -//! Reports, logs, and `--json` keep an error's English exactly as `{error:#}` -//! prints its chain. A front end showing another language translates the first -//! cause in the chain that has a message code, and keeps the English as detail. +//! Reports and `--json` keep an error's English as `{error:#}` prints its +//! chain, less any link that only repeats the one before it. A front end +//! showing another language translates the first cause in the chain that has a +//! message code, and keeps the English as detail. use std::fmt; @@ -44,13 +45,27 @@ impl std::error::Error for Coded {} /// The message for `error`: its English chain, translated through the first /// cause that has a message code. pub fn of_error(error: &anyhow::Error) -> Message { - let text = format!("{error:#}"); + let text = chain_text(error); match error.chain().find_map(code) { Some(msg) => Message::with_text(msg, text), None => Message::raw(text), } } +/// `{error:#}`, without a link that only repeats the one before it: a wrapper +/// that shows its inner error's message and also returns that error as its +/// source would otherwise print the message twice. +fn chain_text(error: &anyhow::Error) -> String { + let mut links: Vec = Vec::new(); + for cause in error.chain() { + let text = cause.to_string(); + if links.last() != Some(&text) { + links.push(text); + } + } + links.join(": ") +} + fn code(cause: &(dyn std::error::Error + 'static)) -> Option { use senbei_engine::android::{ExtractionError, LimitExceeded, RestoreError}; use senbei_metadata::android::{Error as TokenError, MethodIndexError}; @@ -169,4 +184,31 @@ mod tests { Some(&Msg::UnsupportedMetadataVersion { version: 27 }) ); } + + #[test] + fn a_wrapper_that_repeats_its_source_is_printed_once() { + // Shows its inner error's message and returns that error as its source. + #[derive(Debug)] + struct Wrapper(anyhow::Error); + impl std::fmt::Display for Wrapper { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(formatter) + } + } + impl std::error::Error for Wrapper { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(self.0.as_ref()) + } + } + let inner = anyhow::anyhow!("6 unresolved slots").context("restore library"); + let error = anyhow::Error::new(Wrapper(inner)); + assert_eq!( + format!("{error:#}"), + "restore library: restore library: 6 unresolved slots" + ); + assert_eq!( + of_error(&error).text(), + "restore library: 6 unresolved slots" + ); + } }