From 557440b4ea4fa96098ecff03a3f93c1d1c002f3a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 19 Oct 2026 13:51:42 +0200 Subject: [PATCH] Report CLI errors without a secondary PowerShell exception --- TASKS.md | 1 + cc.ps1 | 2 +- tests/Cli.Tests.ps1 | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/Cli.Tests.ps1 diff --git a/TASKS.md b/TASKS.md index 4d89bb7..dcdd2da 100644 --- a/TASKS.md +++ b/TASKS.md @@ -130,3 +130,4 @@ open until it is run on disposable machines with the required account setups. - [x] Apply the force-by-default policy during install/bootstrap too, with a preserve-local-configs opt-out. - [x] Replace bootstrap exit-code exceptions with friendly partial-success, reboot, and busy summaries while preserving automation status. - [x] Avoid null temporary-file cleanup after registry deployment or config adoption; preserve original per-item errors. +- [x] Report CLI exceptions concisely on stderr without throwing from the error handler; preserve exit code 2 and JSON errors. diff --git a/cc.ps1 b/cc.ps1 index c950b10..e2cdfc6 100644 --- a/cc.ps1 +++ b/cc.ps1 @@ -41,6 +41,6 @@ try { } catch { $errorResult = [ordered]@{ command = $Command; succeeded = $false; exitCode = 2; error = $_.Exception.Message } - if ($Json) { $errorResult | ConvertTo-Json -Depth 8 } else { Write-Error $errorResult.error } + if ($Json) { $errorResult | ConvertTo-Json -Depth 8 } else { [Console]::Error.WriteLine("Config Central could not complete: " + $errorResult.error) } exit 2 } diff --git a/tests/Cli.Tests.ps1 b/tests/Cli.Tests.ps1 new file mode 100644 index 0000000..1f5042a --- /dev/null +++ b/tests/Cli.Tests.ps1 @@ -0,0 +1,17 @@ +Describe 'CLI error reporting' { + It 'prints a concise error and preserves exit code 2' { + $output = & (Join-Path $PSHOME $(if ($IsWindows) { 'pwsh.exe' } else { 'pwsh' })) -NoProfile -File "$PSScriptRoot/../cc.ps1" plan -Machine missing-test-machine -DataRoot "$TestDrive/data" 2>&1 + $LASTEXITCODE | Should -Be 2 + ($output | Out-String) | Should -Match 'Config Central could not complete:' + ($output | Out-String) | Should -Not -Match 'Write-Error:|CategoryInfo|FullyQualifiedErrorId' + } + + It 'keeps structured errors available to automation' { + $output = & (Join-Path $PSHOME $(if ($IsWindows) { 'pwsh.exe' } else { 'pwsh' })) -NoProfile -File "$PSScriptRoot/../cc.ps1" plan -Machine missing-test-machine -DataRoot "$TestDrive/data" -Json + $LASTEXITCODE | Should -Be 2 + $result = $output | ConvertFrom-Json + $result.succeeded | Should -BeFalse + $result.exitCode | Should -Be 2 + $result.error | Should -Not -BeNullOrEmpty + } +}