mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
Merge pull request #20933 from n1hility/improve-ps-errors
Improve error handling in win-lib.ps1
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
|
||||
# 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'"
|
||||
}
|
||||
|
Reference in New Issue
Block a user