Skip to content

Commit

Permalink
Improve error handling in win-lib.ps1
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
n1hility committed Dec 7, 2023
1 parent 605a29a commit 3d74067
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions contrib/cirrus/win-lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}

Expand All @@ -68,5 +72,5 @@ function Run-Command {
Write-Host $command

Invoke-Expression $command
Check-Exit
Check-Exit 2 "'$command'"
}

0 comments on commit 3d74067

Please sign in to comment.