diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/input.rs | 2 | ||||
| -rw-r--r-- | src/render/mod.rs | 2 | ||||
| -rw-r--r-- | src/utils/mod.rs | 24 | ||||
| -rw-r--r-- | src/utils/path.rs | 2 |
4 files changed, 22 insertions, 8 deletions
diff --git a/src/config/input.rs b/src/config/input.rs index bf092de..54b2bb0 100644 --- a/src/config/input.rs +++ b/src/config/input.rs @@ -83,7 +83,7 @@ impl Input { } let ret = load_documents(config, local_paths, remote_urls).await; spinner.stop(); - let (files, medias, data_urls) = ret?; + let (files, medias, data_urls) = ret.context("Failed to load files")?; let mut texts = vec![]; if !text.is_empty() { texts.push(text.to_string()); diff --git a/src/render/mod.rs b/src/render/mod.rs index b2ed55d..97161f8 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -26,5 +26,5 @@ pub async fn render_stream( } pub fn render_error(err: anyhow::Error) { - eprint!("{}", error_text(&pretty_error(&err))); + eprintln!("{}", error_text(&pretty_error(&err))); } 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?; |
