Skip to content

Commit 9730bd0

Browse files
committed
refactor: [#107] apply GitHub Copilot PR review suggestions
- Fix error mapping: Use CreateSubcommandError::UserOutputLockFailed instead of ProgressReporterError::UserOutputMutexPoisoned - Eliminate unnecessary clones: Use ctx.user_output().clone() directly for ProgressReporter initialization - Enhance panic message with detailed context about UserOutput lock poisoning - Document fallback error formatting rationale in handle_error() All changes address PR #108 review comments from GitHub Copilot.
1 parent b49bcc0 commit 9730bd0

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/presentation/commands/create/subcommands/environment.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn handle_environment_creation(
5454
let ctx = factory.create_context(working_dir.to_path_buf(), user_output.clone());
5555

5656
// Create progress reporter for 3 main steps
57-
let mut progress = ProgressReporter::new(user_output.clone(), 3);
57+
let mut progress = ProgressReporter::new(ctx.user_output().clone(), 3);
5858

5959
// Step 1: Load configuration
6060
progress.start_step("Loading configuration")?;
@@ -117,7 +117,7 @@ fn load_configuration(
117117
) -> Result<EnvironmentCreationConfig, CreateSubcommandError> {
118118
user_output
119119
.lock()
120-
.map_err(|_| crate::presentation::progress::ProgressReporterError::UserOutputMutexPoisoned)?
120+
.map_err(|_| CreateSubcommandError::UserOutputLockFailed)?
121121
.progress(&format!(
122122
"Loading configuration from '{}'...",
123123
env_file.display()
@@ -159,15 +159,15 @@ fn execute_create_command(
159159
) -> Result<Environment, CreateSubcommandError> {
160160
user_output
161161
.lock()
162-
.map_err(|_| crate::presentation::progress::ProgressReporterError::UserOutputMutexPoisoned)?
162+
.map_err(|_| CreateSubcommandError::UserOutputLockFailed)?
163163
.progress(&format!(
164164
"Creating environment '{}'...",
165165
config.environment.name
166166
));
167167

168168
user_output
169169
.lock()
170-
.map_err(|_| crate::presentation::progress::ProgressReporterError::UserOutputMutexPoisoned)?
170+
.map_err(|_| CreateSubcommandError::UserOutputLockFailed)?
171171
.progress("Validating configuration and creating environment...");
172172

173173
#[allow(clippy::manual_inspect)]
@@ -199,10 +199,16 @@ fn execute_create_command(
199199
/// This function will panic if the `UserOutput` mutex is poisoned. Since this is
200200
/// called after successful environment creation (when operation is complete),
201201
/// a poisoned mutex indicates an irrecoverable state and panicking is acceptable.
202+
///
203+
/// The panic message provides detailed context matching our error handling principles:
204+
/// clear explanation of what happened, why it's critical, and that it indicates a bug.
202205
fn display_creation_results(user_output: &Arc<Mutex<UserOutput>>, environment: &Environment) {
203-
let mut output = user_output
204-
.lock()
205-
.expect("UserOutput mutex poisoned after successful environment creation");
206+
let mut output = user_output.lock().expect(
207+
"CRITICAL: UserOutput mutex poisoned after successful environment creation. \
208+
This indicates a panic occurred in another thread while holding the output lock. \
209+
The environment was created successfully, but we cannot display the results. \
210+
This is a bug - please report it with full logs using --log-output file-and-stderr",
211+
);
206212

207213
output.success(&format!(
208214
"Environment '{}' created successfully",

src/presentation/commands/destroy/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn handle_destroy_command(
6565
let ctx = factory.create_context(working_dir.to_path_buf(), user_output.clone());
6666

6767
// Create progress reporter for 3 main steps
68-
let mut progress = ProgressReporter::new(user_output.clone(), 3);
68+
let mut progress = ProgressReporter::new(ctx.user_output().clone(), 3);
6969

7070
// Step 1: Validate environment name
7171
progress.start_step("Validating environment")?;

src/presentation/commands/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ pub fn handle_error(error: &CommandError, user_output: &Arc<Mutex<UserOutput>>)
144144
output.info_block("For detailed troubleshooting:", &[help_text]);
145145
} else {
146146
// Cannot acquire lock - print to stderr directly as fallback
147+
//
148+
// RATIONALE: Plain text formatting without emojis/styling is intentional.
149+
// When the mutex is poisoned, we're in a degraded error state where another
150+
// thread has panicked. Using plain eprintln! ensures maximum compatibility
151+
// and avoids any additional complexity that could fail in this critical path.
152+
// The goal here is reliability over aesthetics - get the error message to
153+
// the user no matter what, even if it's not pretty.
147154
eprintln!("ERROR: {error}");
148155
eprintln!();
149156
eprintln!("CRITICAL: Failed to acquire user output lock.");

0 commit comments

Comments
 (0)