summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-10-23 17:23:40 +0800
committerGitHub <noreply@github.com>2024-10-23 17:23:40 +0800
commit989debe8d7438028245b965b9d464032a0d3e0a3 (patch)
tree753a0c298b4933ea9fd38aed450d01d2103d4f64 /src/utils
parent5c06c67ee45e95375eae2e1727e41603fdb9b2f3 (diff)
downloadaichat-989debe8d7438028245b965b9d464032a0d3e0a3.tar.gz
refactor: improve error rendering (#948)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mod.rs24
-rw-r--r--src/utils/path.rs2
2 files changed, 20 insertions, 6 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index a551855..27dac4a 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -145,16 +145,30 @@ pub fn fuzzy_match(text: &str, pattern: &str) -> bool {
pub fn pretty_error(err: &anyhow::Error) -> String {
let mut output = vec![];
output.push(format!("Error: {err}"));
- for (i, cause) in err.chain().skip(1).enumerate() {
- if i == 0 {
- output.push("Caused by:".to_string());
+ let causes: Vec<_> = err.chain().skip(1).collect();
+ let causes_len = causes.len();
+ if causes_len > 0 {
+ output.push("\nCaused by:".to_string());
+ if causes_len == 1 {
+ output.push(format!(" {}", indent_text(causes[0], 4).trim()));
+ } else {
+ for (i, cause) in causes.into_iter().enumerate() {
+ output.push(format!("{i:5}: {}", indent_text(cause, 7).trim()));
+ }
}
- output.push(format!(" {i}: {cause}"));
}
- output.push(String::new());
output.join("\n")
}
+pub fn indent_text<T: ToString>(s: T, size: usize) -> String {
+ let indent_str = " ".repeat(size);
+ s.to_string()
+ .split('\n')
+ .map(|line| format!("{}{}", indent_str, line))
+ .collect::<Vec<String>>()
+ .join("\n")
+}
+
pub fn error_text(input: &str) -> String {
color_text(input, nu_ansi_term::Color::Red)
}
diff --git a/src/utils/path.rs b/src/utils/path.rs
index e27b5ee..a8534fb 100644
--- a/src/utils/path.rs
+++ b/src/utils/path.rs
@@ -82,7 +82,7 @@ async fn list_files(
suffixes: Option<&Vec<String>>,
) -> Result<()> {
if !entry_path.exists() {
- bail!("Not found: {}", entry_path.display());
+ bail!("Not found '{}'", entry_path.display());
}
if entry_path.is_dir() {
let mut reader = tokio::fs::read_dir(entry_path).await?;