Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Improve multiline error formatting with context (#249)

Maximilian Roosf0572d9a9595
diff --git a/src/commands/context.rs b/src/commands/context.rs
index 3395750b5..3b72b516f 100644
--- a/src/commands/context.rs
+++ b/src/commands/context.rs
@@ -34,7 +34,9 @@ impl CommandEnv {
let worktree_path = std::env::current_dir().context("Failed to get current directory")?;
let branch = repo.require_current_branch(action)?;
let config = WorktrunkConfig::load().context("Failed to load config")?;
- let repo_root = repo.worktree_base()?;
+ let repo_root = repo
+ .worktree_base()
+ .context("Failed to determine repository root")?;
Ok(Self {
repo,
@@ -57,7 +59,9 @@ impl CommandEnv {
.current_branch()
.context("Failed to determine current branch")?;
let config = WorktrunkConfig::load().context("Failed to load config")?;
- let repo_root = repo.worktree_base()?;
+ let repo_root = repo
+ .worktree_base()
+ .context("Failed to determine repository root")?;
Ok(Self {
repo,
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index a41a066da..4432f22ac 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -19,6 +19,7 @@
//! - Within worktrees: Git operations (ahead/behind, diffs, CI) run concurrently via scoped threads
//!
//! This ensures fast operations don't wait for slow ones (e.g., CI doesn't block ahead/behind counts)
+use anyhow::Context;
use color_print::cformat;
use crossbeam_channel as chan;
use dunce::canonicalize;
@@ -590,12 +591,14 @@ pub fn collect(
) -> anyhow::Result<Option<super::model::ListData>> {
use super::progressive_table::ProgressiveTable;
- let worktrees = repo.list_worktrees()?;
+ let worktrees = repo.list_worktrees().context("Failed to list worktrees")?;
if worktrees.worktrees.is_empty() {
return Ok(None);
}
- let default_branch = repo.default_branch()?;
+ let default_branch = repo
+ .default_branch()
+ .context("Failed to determine default branch")?;
// Effective target for integration checks: upstream if ahead of local, else local.
// This handles the case where a branch was merged remotely but user hasn't pulled yet.
let integration_target = repo.effective_integration_target(&default_branch);
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 81e93e192..20fdb14f8 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -110,6 +110,7 @@
//!
//! The shell wrapper is generated by `wt config shell init <shell>` from templates in `templates/`.
+use anyhow::Context;
use color_print::cformat;
use dunce::canonicalize;
use normalize_path::NormalizePath;
@@ -446,7 +447,9 @@ pub fn handle_switch(
let actual_current_branch = repo.current_branch().ok().flatten();
// Resolve special branch names ("@" for current, "-" for previous)
- let resolved_branch = repo.resolve_worktree_name(branch)?;
+ let resolved_branch = repo
+ .resolve_worktree_name(branch)
+ .context("Failed to resolve branch name")?;
// Record actual current branch as new "previous" for ping-pong behavior
let new_previous = actual_current_branch;
@@ -767,10 +770,14 @@ pub fn handle_remove_by_path(
// Check that the worktree is clean
let target_repo = Repository::at(path);
- target_repo.ensure_clean_working_tree(Some("remove worktree"), branch.as_deref())?;
+ target_repo
+ .ensure_clean_working_tree(Some("remove worktree"), branch.as_deref())
+ .context("Failed to verify worktree status")?;
// We're not in this worktree, so no directory change needed
- let current_path = repo.worktree_root()?;
+ let current_path = repo
+ .worktree_root()
+ .context("Failed to determine current worktree")?;
Ok(RemoveResult::RemovedWorktree {
main_path: current_path,
diff --git a/src/main.rs b/src/main.rs
index 51d51efc0..5f8bee750 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1510,8 +1510,6 @@ fn main() {
let _ = output::terminate_output();
if let Err(e) = result {
- use worktrunk::styling::ERROR_EMOJI;
-
// GitError, WorktrunkError, and HookErrorWithHint produce styled output via Display
if let Some(err) = e.downcast_ref::<worktrunk::git::GitError>() {
let _ = output::print(err.to_string());
@@ -1526,14 +1524,25 @@ fn main() {
// - Empty error: skip (errors already printed elsewhere)
let msg = e.to_string();
if !msg.is_empty() {
- let root_cause = e.root_cause().to_string();
- if msg != root_cause {
- // Has context: msg is context, root_cause is underlying error
+ // Collect the error chain (skipping the first which is in msg)
+ let chain: Vec<String> = e.chain().skip(1).map(|e| e.to_string()).collect();
+ if !chain.is_empty() {
+ // Has context: msg is context, chain contains intermediate + root cause
let _ = output::print(error_message(&msg));
- let _ = output::gutter(format_with_gutter(&root_cause, "", None));
+ let chain_text = chain.join("\n");
+ let _ = output::gutter(format_with_gutter(&chain_text, "", None));
+ } else if msg.contains('\n') {
+ // Multiline error without context - this shouldn't happen if all
+ // errors have proper context. Fail in tests, log in production.
+ if cfg!(test) {
+ panic!("Multiline error without context: {msg}");
+ }
+ log::warn!("Multiline error without context: {msg}");
+ let _ = output::print(error_message("Command failed"));
+ let _ = output::gutter(format_with_gutter(&msg, "", None));
} else {
- // Simple error: inline with emoji
- let _ = output::print(cformat!("{ERROR_EMOJI} <red>{msg}</>"));
+ // Single-line error without context: inline with emoji
+ let _ = output::print(error_message(&msg));
}
}
}