Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve error rendering #948

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
24 changes: 19 additions & 5 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down