Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Format GitError enum for better readability
Maximilian Roosfff05f1d36cf
diff --git a/src/git/error.rs b/src/git/error.rsindex d4a2f2bd0..731207168 100644--- a/src/git/error.rs+++ b/src/git/error.rs@@ -17,10 +17,6 @@ use crate::styling::{ERROR, ERROR_BOLD, ERROR_EMOJI, HINT, HINT_BOLD, HINT_EMOJI, INFO_EMOJI, format_with_gutter,};-// =============================================================================-// GitError - Typed domain errors-// =============================================================================-/// Domain errors for git and worktree operations.////// This enum provides structured error data that can be pattern-matched and tested.@@ -40,113 +36,77 @@ use crate::styling::{/// ```#[derive(Debug, Clone)]pub enum GitError {- // -------------------------------------------------------------------------// Git state errors- // -------------------------------------------------------------------------- /// HEAD is detached (not on a branch)DetachedHead {- /// Action that was blocked (e.g., "merge", "push")action: Option<String>,},-- /// Working tree has uncommitted changesUncommittedChanges {- /// Action that was blocked (e.g., "remove worktree")action: Option<String>,},+ BranchAlreadyExists {+ branch: String,+ },- /// Branch already exists- BranchAlreadyExists { branch: String },-- // -------------------------------------------------------------------------// Worktree errors- // -------------------------------------------------------------------------- /// Worktree directory is missing from filesystem- WorktreeMissing { branch: String },-- /// No worktree exists for the given branch- NoWorktreeFound { branch: String },-- /// Target path for worktree is already occupied+ WorktreeMissing {+ branch: String,+ },+ NoWorktreeFound {+ branch: String,+ },WorktreePathOccupied {branch: String,path: PathBuf,- /// Branch currently using this path, if knownoccupant: Option<String>,},-- /// Directory already exists at target path- WorktreePathExists { path: PathBuf },-- /// Failed to create worktree+ WorktreePathExists {+ path: PathBuf,+ },WorktreeCreationFailed {branch: String,base_branch: Option<String>,error: String,},-- /// Failed to remove worktreeWorktreeRemovalFailed {branch: String,path: PathBuf,error: String,},- // -------------------------------------------------------------------------// Merge/push errors- // -------------------------------------------------------------------------- /// Conflicting uncommitted changes prevent operationConflictingChanges {files: Vec<String>,worktree_path: PathBuf,},-- /// Push is not fast-forwardNotFastForward {target_branch: String,- /// Formatted commit list for displaycommits_formatted: String,- /// Whether this error occurred during a merge operation (affects hint message)in_merge_context: bool,},-- /// Merge commits found in push rangeMergeCommitsFound,-- /// Rebase stopped due to conflictsRebaseConflict {target_branch: String,git_output: String,},+ PushFailed {+ error: String,+ },- /// Push operation failed- PushFailed { error: String },-- // -------------------------------------------------------------------------- // Validation errors- // -------------------------------------------------------------------------- /// Cannot prompt in non-interactive environment+ // Validation/other errorsNotInteractive,-- /// Parse error (for git output parsing failures)- ParseError { message: String },-- /// LLM command failed during commit generation+ ParseError {+ message: String,+ },LlmCommandFailed {- /// The full command string (e.g., "llm --model claude")command: String,- /// The stderr output from the commanderror: String,},-- /// Project configuration file not foundProjectConfigNotFound {- /// Path where config file was expectedconfig_path: PathBuf,},-- /// Generic error with custom message- Other { message: String },+ Other {+ message: String,+ },}impl std::error::Error for GitError {}@@ -358,10 +318,6 @@ impl std::fmt::Display for GitError {}}-// =============================================================================-// WorktrunkError - Semantic errors with special handling-// =============================================================================-/// Semantic errors that require special handling in main.rs////// Most errors use anyhow::bail! with formatted messages. This enum is only@@ -427,10 +383,6 @@ pub fn is_command_not_approved(err: &anyhow::Error) -> bool {.is_some_and(|e| matches!(e, WorktrunkError::CommandNotApproved))}-// =============================================================================-// Error formatting helpers-// =============================================================================-/// Format an error with header and gutter contentfn format_error_block(header: String, error: &str) -> String {let trimmed = error.trim();@@ -441,10 +393,6 @@ fn format_error_block(header: String, error: &str) -> String {}}-// =============================================================================-// Tests-// =============================================================================-#[cfg(test)]mod tests {use super::*;diff --git a/src/main.rs b/src/main.rsindex 939b2ac5a..b132b89d8 100644--- a/src/main.rs+++ b/src/main.rs@@ -784,12 +784,12 @@ fn main() {use worktrunk::styling::{ERROR, ERROR_EMOJI};// GitError and WorktrunkError produce styled output via Display- if let Some(git_err) = e.downcast_ref::<worktrunk::git::GitError>() {- let _ = output::print(git_err.to_string());- } else if let Some(wt_err) = e.downcast_ref::<worktrunk::git::WorktrunkError>() {- let _ = output::print(wt_err.to_string());+ if let Some(err) = e.downcast_ref::<worktrunk::git::GitError>() {+ let _ = output::print(err.to_string());+ } else if let Some(err) = e.downcast_ref::<worktrunk::git::WorktrunkError>() {+ let _ = output::print(err.to_string());} else {- // Anyhow error - show context message, multi-line root cause gets gutter+ // Anyhow error - format with emoji, multi-line root cause gets gutterlet msg = e.to_string();let root_cause = e.root_cause().to_string();let _ = output::print(format!("{ERROR_EMOJI} {ERROR}{msg}{ERROR:#}"));