mirror of
https://github.com/containers/podman.git
synced 2025-07-03 17:27:18 +08:00
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 <jason.greene@redhat.com>
This commit is contained in:
@ -46,12 +46,16 @@ if ($Env:CI -eq "true") {
|
|||||||
# (builtins)! They set '$?' to "True" (failed) or "False" success so calling
|
# (builtins)! They set '$?' to "True" (failed) or "False" success so calling
|
||||||
# this would mask failures. Rely on $ErrorActionPreference = 'Stop' instead.
|
# this would mask failures. Rely on $ErrorActionPreference = 'Stop' instead.
|
||||||
function Check-Exit {
|
function Check-Exit {
|
||||||
|
param (
|
||||||
|
[int] $stackPos = 1,
|
||||||
|
[string] $command = 'command'
|
||||||
|
)
|
||||||
|
|
||||||
$result = $LASTEXITCODE # WARNING: might not be a number!
|
$result = $LASTEXITCODE # WARNING: might not be a number!
|
||||||
if ( ($result -ne $null) -and ($result -ne 0) ) {
|
if ( ($result -ne $null) -and ($result -ne 0) ) {
|
||||||
# https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe
|
# https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe
|
||||||
$caller = (Get-PSCallStack)[1]
|
$caller = (Get-PSCallStack)[$stackPos]
|
||||||
Write-Host "Exit code = '$result' from $($caller.ScriptName):$($caller.ScriptLineNumber)"
|
throw "Exit code = '$result' running $command at $($caller.ScriptName):$($caller.ScriptLineNumber)"
|
||||||
Throw "Non-zero exit code"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,5 +72,5 @@ function Run-Command {
|
|||||||
Write-Host $command
|
Write-Host $command
|
||||||
|
|
||||||
Invoke-Expression $command
|
Invoke-Expression $command
|
||||||
Check-Exit
|
Check-Exit 2 "'$command'"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user