Skip to content

Commit ced97f3

Browse files
branchseerclaude
andcommitted
refactor: change post_execution and execute return type to Result<(), ExitCode>
Use Result type for clearer success/failure semantics: - Ok(()) for success (exit code 0) - Err(ExitCode) for failure with specific exit code Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4596350 commit ced97f3

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

crates/vite_task/src/session/execute/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,12 @@ impl ExecutionContext<'_> {
330330
impl<'a, CustomSubcommand> Session<'a, CustomSubcommand> {
331331
/// Execute an execution plan, reporting events to the provided reporter.
332332
///
333-
/// Returns the ExitCode from the reporter's post_execution method.
334-
pub async fn execute(&self, plan: ExecutionPlan, mut reporter: Box<dyn Reporter>) -> ExitCode {
333+
/// Returns Ok(()) on success, or Err(ExitCode) on failure.
334+
pub async fn execute(
335+
&self,
336+
plan: ExecutionPlan,
337+
mut reporter: Box<dyn Reporter>,
338+
) -> Result<(), ExitCode> {
335339
let mut execution_context = ExecutionContext {
336340
event_handler: &mut *reporter,
337341
current_execution_id: ExecutionId::zero(),

crates/vite_task/src/session/reporter.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub trait Reporter {
3535
fn handle_event(&mut self, event: ExecutionEvent);
3636

3737
/// Called after execution completes (whether successful or not)
38-
/// Returns an ExitCode that is a suggestion for process exit
39-
fn post_execution(self: Box<Self>) -> ExitCode;
38+
/// Returns Ok(()) on success, or Err(ExitCode) on failure
39+
fn post_execution(self: Box<Self>) -> Result<(), ExitCode>;
4040
}
4141

4242
const COMMAND_STYLE: Style = Style::new().cyan();
@@ -487,7 +487,7 @@ impl<W: Write> Reporter for LabeledReporter<W> {
487487
}
488488
}
489489

490-
fn post_execution(mut self: Box<Self>) -> ExitCode {
490+
fn post_execution(mut self: Box<Self>) -> Result<(), ExitCode> {
491491
// Check if execution was aborted due to error
492492
if let Some(error_msg) = &self.first_error {
493493
// Print separator
@@ -514,7 +514,7 @@ impl<W: Write> Reporter for LabeledReporter<W> {
514514
.style(Style::new().bright_black())
515515
);
516516

517-
return ExitCode::FAILURE;
517+
return Err(ExitCode::FAILURE);
518518
}
519519

520520
// No errors - print summary if not hidden
@@ -528,9 +528,9 @@ impl<W: Write> Reporter for LabeledReporter<W> {
528528
}
529529

530530
// Determine exit code based on failed tasks:
531-
// 1. All tasks succeed → return 0
532-
// 2. Exactly one task failed → return that task's exit code
533-
// 3. More than one task failed → return 1
531+
// 1. All tasks succeed → return Ok(())
532+
// 2. Exactly one task failed → return Err with that task's exit code
533+
// 3. More than one task failed → return Err(1)
534534
let failed_exit_codes: Vec<i32> = self
535535
.executions
536536
.iter()
@@ -539,13 +539,13 @@ impl<W: Write> Reporter for LabeledReporter<W> {
539539
.collect();
540540

541541
match failed_exit_codes.len() {
542-
0 => ExitCode::SUCCESS,
542+
0 => Ok(()),
543543
1 => {
544544
// Return the single failed task's exit code (clamped to u8 range)
545545
let code = failed_exit_codes[0];
546-
ExitCode::from(code.clamp(1, 255) as u8)
546+
Err(ExitCode::from(code.clamp(1, 255) as u8))
547547
}
548-
_ => ExitCode::FAILURE,
548+
_ => Err(ExitCode::FAILURE),
549549
}
550550
}
551551
}

crates/vite_task_bin/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ async fn run() -> anyhow::Result<ExitCode> {
4040

4141
// Create reporter and execute
4242
let reporter = LabeledReporter::new(std::io::stdout(), session.workspace_path());
43-
let exit_code = session.execute(plan, Box::new(reporter)).await;
44-
45-
Ok(exit_code)
43+
Ok(session.execute(plan, Box::new(reporter)).await.err().unwrap_or(ExitCode::SUCCESS))
4644
}

0 commit comments

Comments
 (0)