From 3d740674b3d1be9e612ec0988c8bd9623456ea20 Mon Sep 17 00:00:00 2001 From: "Jason T. Greene" Date: Wed, 6 Dec 2023 21:41:26 -0600 Subject: [PATCH] Improve error handling in win-lib.ps1 - Modified Check-Exit to take a relative stack postition so that reusing functions like Run-Command report on their callers as opposed to the source position of the wrapper. - Record and print the last command executed as it likely scrolled off with test output. [NO NEW TESTS NEEDED] Signed-off-by: Jason T. Greene --- contrib/cirrus/win-lib.ps1 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/contrib/cirrus/win-lib.ps1 b/contrib/cirrus/win-lib.ps1 index 63f0cc32f9..ce22a69e74 100644 --- a/contrib/cirrus/win-lib.ps1 +++ b/contrib/cirrus/win-lib.ps1 @@ -46,12 +46,16 @@ if ($Env:CI -eq "true") { # (builtins)! They set '$?' to "True" (failed) or "False" success so calling # this would mask failures. Rely on $ErrorActionPreference = 'Stop' instead. function Check-Exit { + param ( + [int] $stackPos = 1, + [string] $command = 'command' + ) + $result = $LASTEXITCODE # WARNING: might not be a number! if ( ($result -ne $null) -and ($result -ne 0) ) { # https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe - $caller = (Get-PSCallStack)[1] - Write-Host "Exit code = '$result' from $($caller.ScriptName):$($caller.ScriptLineNumber)" - Throw "Non-zero exit code" + $caller = (Get-PSCallStack)[$stackPos] + throw "Exit code = '$result' running $command at $($caller.ScriptName):$($caller.ScriptLineNumber)" } } @@ -68,5 +72,5 @@ function Run-Command { Write-Host $command Invoke-Expression $command - Check-Exit + Check-Exit 2 "'$command'" }