mirror of
https://github.com/Momoko-Ayase/Senbei.git
synced 2026-11-25 02:52:47 +08:00
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.
This commit is contained in:
1 parent
b2286dec1c
commit
7a1f3c52d5
1 file changed
+46
-4
@@ -1,8 +1,9 @@
|
|||||||
//! Translatable messages for errors that reach the I/O layer.
|
//! Translatable messages for errors that reach the I/O layer.
|
||||||
//!
|
//!
|
||||||
//! Reports, logs, and `--json` keep an error's English exactly as `{error:#}`
|
//! Reports and `--json` keep an error's English as `{error:#}` prints its
|
||||||
//! prints its chain. A front end showing another language translates the first
|
//! chain, less any link that only repeats the one before it. A front end
|
||||||
//! cause in the chain that has a message code, and keeps the English as detail.
|
//! showing another language translates the first cause in the chain that has a
|
||||||
|
//! message code, and keeps the English as detail.
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
@@ -44,13 +45,27 @@ impl std::error::Error for Coded {}
|
|||||||
/// The message for `error`: its English chain, translated through the first
|
/// The message for `error`: its English chain, translated through the first
|
||||||
/// cause that has a message code.
|
/// cause that has a message code.
|
||||||
pub fn of_error(error: &anyhow::Error) -> Message {
|
pub fn of_error(error: &anyhow::Error) -> Message {
|
||||||
let text = format!("{error:#}");
|
let text = chain_text(error);
|
||||||
match error.chain().find_map(code) {
|
match error.chain().find_map(code) {
|
||||||
Some(msg) => Message::with_text(msg, text),
|
Some(msg) => Message::with_text(msg, text),
|
||||||
None => Message::raw(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<String> = 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<Msg> {
|
fn code(cause: &(dyn std::error::Error + 'static)) -> Option<Msg> {
|
||||||
use senbei_engine::android::{ExtractionError, LimitExceeded, RestoreError};
|
use senbei_engine::android::{ExtractionError, LimitExceeded, RestoreError};
|
||||||
use senbei_metadata::android::{Error as TokenError, MethodIndexError};
|
use senbei_metadata::android::{Error as TokenError, MethodIndexError};
|
||||||
@@ -169,4 +184,31 @@ mod tests {
|
|||||||
Some(&Msg::UnsupportedMetadataVersion { version: 27 })
|
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"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user