From dded2da3a750d216a7b671439190e8abc1dd3993 Mon Sep 17 00:00:00 2001 From: Jeffrey <9938813+Raphire@users.noreply.github.com> Date: Sat, 15 Aug 2026 17:59:24 +0200 Subject: [PATCH] Clean up & simplify error handling and reporting --- Scripts/Features/Invoke-Changes.ps1 | 21 +++------------------ Scripts/GUI/Show-ApplyModal.ps1 | 18 +++--------------- Tests/Invoke-Changes.Tests.ps1 | 10 +++------- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/Scripts/Features/Invoke-Changes.ps1 b/Scripts/Features/Invoke-Changes.ps1 index 3e496dc..f3bdb65 100644 --- a/Scripts/Features/Invoke-Changes.ps1 +++ b/Scripts/Features/Invoke-Changes.ps1 @@ -272,7 +272,6 @@ function Invoke-ApplyFeatures { } if (-not (Invoke-FeatureApply -FeatureId $featureId)) { - $script:ApplyFeatureFailures++ $script:FeatureFailures++ } Write-Host "" @@ -315,7 +314,6 @@ function Invoke-UndoFeatures { } if (-not (Invoke-FeatureUndo -FeatureId $featureId)) { - $script:UndoFeatureFailures++ $script:FeatureFailures++ } Write-Host "" @@ -349,9 +347,6 @@ function Invoke-AllChanges { $script:AppRemovalFailures = 0 $script:FeatureFailures = 0 - $script:ApplyFeatureFailures = 0 - $script:UndoFeatureFailures = 0 - $script:PrerequisiteFailures = 0 $script:AppRemovalVerificationUnavailable = $false # ---- Gather work items ---- @@ -434,7 +429,7 @@ function Invoke-AllChanges { $restorePointSucceeded = Invoke-SystemRestorePoint if (-not $restorePointSucceeded) { if ($script:CancelRequested) { return } - $script:PrerequisiteFailures++ + $script:FeatureFailures++ } Write-Host "" } @@ -466,19 +461,9 @@ function Invoke-AllChanges { Write-Warning "$($script:AppRemovalFailures) app removal(s) failed. See output above for details." } - if ($script:ApplyFeatureFailures -gt 0) { + if ($script:FeatureFailures -gt 0) { Write-Host "" - Write-Warning "$($script:ApplyFeatureFailures) feature change(s) failed to apply. See output above for details." - } - - if ($script:UndoFeatureFailures -gt 0) { - Write-Host "" - Write-Warning "$($script:UndoFeatureFailures) feature change(s) failed to undo. See output above for details." - } - - if ($script:PrerequisiteFailures -gt 0) { - Write-Host "" - Write-Warning "$($script:PrerequisiteFailures) requested prerequisite(s) could not be completed. Changes continued at your request." + Write-Warning "$($script:FeatureFailures) feature change(s) failed. See output above for details." } if ($script:AppRemovalVerificationUnavailable) { diff --git a/Scripts/GUI/Show-ApplyModal.ps1 b/Scripts/GUI/Show-ApplyModal.ps1 index 144d2ec..f4870d7 100644 --- a/Scripts/GUI/Show-ApplyModal.ps1 +++ b/Scripts/GUI/Show-ApplyModal.ps1 @@ -114,13 +114,7 @@ function Show-ApplyModal { try { Invoke-AllChanges - $appRemovalFailureCount = [int]$script:AppRemovalFailures - $applyFailureCount = [int]$script:ApplyFeatureFailures - $undoFailureCount = [int]$script:UndoFeatureFailures - $featureFailureCount = $applyFailureCount + $undoFailureCount - $prerequisiteFailureCount = [int]$script:PrerequisiteFailures - # App removals are a subset of failed features, so adding both counters - # would report each affected feature twice. + $featureFailureCount = [int]$script:FeatureFailures $failureCount = $featureFailureCount $appRemovalVerificationUnavailable = [bool]$script:AppRemovalVerificationUnavailable @@ -149,7 +143,7 @@ function Show-ApplyModal { $script:ApplyCompletionIconEl.Foreground = [System.Windows.Media.SolidColorBrush]::new([System.Windows.Media.ColorConverter]::ConvertFromString("#e8912d")) $script:ApplyCompletionTitleEl.Text = "Cancelled" $script:ApplyCompletionMessageEl.Text = "Script execution was cancelled by the user." - } elseif ($failureCount -gt 0 -or $prerequisiteFailureCount -gt 0 -or $appRemovalVerificationUnavailable) { + } elseif ($failureCount -gt 0 -or $appRemovalVerificationUnavailable) { if ($failureCount -gt 0) { Write-Host "Script completed with $failureCount error(s)." } @@ -162,13 +156,7 @@ function Show-ApplyModal { } else { $script:ApplyCompletionTitleEl.Text = "Changes Applied with Errors" - $failureMessages = @() - if ($appRemovalFailureCount -gt 0) { $failureMessages += "$appRemovalFailureCount app removal(s) failed" } - if ($applyFailureCount -gt 0) { $failureMessages += "$applyFailureCount feature change(s) failed to apply" } - if ($undoFailureCount -gt 0) { $failureMessages += "$undoFailureCount feature change(s) failed to undo" } - if ($prerequisiteFailureCount -gt 0) { $failureMessages += "$prerequisiteFailureCount requested prerequisite(s) could not be completed" } - if ($appRemovalVerificationUnavailable) { $failureMessages += "Unable to verify if all apps were uninstalled successfully" } - $script:ApplyCompletionMessageEl.Text = "$($failureMessages -join '; '). See console for details." + $script:ApplyCompletionMessageEl.Text = "$featureFailureCount change(s) failed. See console for details." } } else { Write-Host "All changes have been applied successfully!" diff --git a/Tests/Invoke-Changes.Tests.ps1 b/Tests/Invoke-Changes.Tests.ps1 index 3e24bfe..6c77100 100644 --- a/Tests/Invoke-Changes.Tests.ps1 +++ b/Tests/Invoke-Changes.Tests.ps1 @@ -263,7 +263,6 @@ Describe 'Invoke-ApplyFeatures' { It 'counts a failed feature application and continues with later features' { $script:FeatureFailures = 0 - $script:ApplyFeatureFailures = 0 Mock Invoke-FeatureApply { param($FeatureId) return ($FeatureId -ne 'One') @@ -272,7 +271,6 @@ Describe 'Invoke-ApplyFeatures' { Invoke-ApplyFeatures -FeatureIds @('One', 'Two') -StartStep 1 -TotalSteps 2 $script:FeatureFailures | Should -Be 1 - $script:ApplyFeatureFailures | Should -Be 1 Should -Invoke Invoke-FeatureApply -Times 2 -Exactly } } @@ -305,13 +303,11 @@ Describe 'Invoke-UndoFeatures' { It 'counts one failure when a feature undo fails' { $script:FeatureFailures = 0 - $script:UndoFeatureFailures = 0 Mock Invoke-FeatureUndo { $false } Invoke-UndoFeatures -FeatureIds @('RegistryUndo') -StartStep 1 -TotalSteps 1 $script:FeatureFailures | Should -Be 1 - $script:UndoFeatureFailures | Should -Be 1 Should -Invoke Invoke-FeatureUndo -Times 1 -Exactly } @@ -484,15 +480,15 @@ Describe 'Invoke-AllChanges' { $script:order | Should -Be @('restore-point', 'apply') } - It 'reports a restore point failure when the user chooses to continue' { + It 'counts a restore point failure as a feature failure when the user chooses to continue' { $script:Params = @{ CreateRestorePoint = $true; CustomApply = $true } $script:UndoParams = @{} Mock Invoke-SystemRestorePoint { $false } Invoke-AllChanges - $script:PrerequisiteFailures | Should -Be 1 - Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'requested prerequisite' } + $script:FeatureFailures | Should -Be 1 + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match '1 feature change\(s\) failed\.' } } It 'reports app removal failures after all requested work completes' {