3

I'm trying to write a PowerShell script which sends a Slack notification whenever our build system in unable to push Docker images to Docker Hub. How can I detect and handle the Process exited with code 1 from the docker push command? I've tried the following, but the catch block isn't called.

try {
  docker push $dockerImage;
}catch{
  #Send error details to Slack
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Yoshi
  • 33
  • 1
  • 3
  • Does this answer your question? [PowerShell and process exit codes](https://stackoverflow.com/questions/57468522/powershell-and-process-exit-codes) – iRon May 28 '21 at 17:38

1 Answers1

4
  • As of PowerShell 7.3, calls to external programs (such as docker) never cause a statement-terminating error[1] that you can trap with try / catch; similarly, stderr output from external program is (rightfully) not considered error output by PowerShell, and therefore $ErrorActionPreference = 'Stop' has no effect on it (for legacy exceptions in Windows PowerShell, see this answer).

    • However, opt-in integration with PowerShell's error handling may be implemented in the future; as of v7.3, there is an experimental feature named PSNativeCommandErrorActionPreference (which may become an official feature):

      • If this feature is enabled (Enable-ExperimentalFeature PSNativeCommandErrorActionPreference) and the associated $PSNativeCommandErrorActionPreference preference variable is set to $true, any external-program call that reports a nonzero exit code automatically triggers a PowerShell error in response (obviating the need for explicit $LASTEXITCODE -ne 0 checks), which then integrates with PowerShell's own error handling.[2]
    • In general, note that a statement-terminating PowerShell error (which can be caught with try / catch) does occur if PowerShell fails to even launch a command, such as when you supply a non-existent executable name or path; e.g.:

      try   { nosuchexe foo bar }
      catch { Write-Warning "Failed to invoke nosuchexe: $_" }
      
  • The automatic $LASTEXITCODE variable reflects the process exit code of the most recently executed external program. By convention, exit code 0 signals success, anything else an error condition.

Therefore:

docker push $dockerImage
if ($LASTEXITCODE -ne 0) {
  #Send error details to Slack
}

See also:

  • This answer has more information on exit-code processing in PowerShell.

[1] The only exception is a bug, present up to PowerShell 7.1, where the combination of redirecting stderr output (2> or *>) with $ErrorActionPreference = 'Stop' unexpectedly causes a script-terminating error if there's actual stderr output - see this answer.

[2] Unfortunately, as of v7.3.0, the automatically triggered PowerShell error is a non-terminating error rather than a statement-terminating one; the latter would make more sense, as it would allow it to be selectively caught with a try statement - see GitHub issue #18368.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I don't think it's a good idea have large random pos/neg numbers as failing result codes from `$LASTEXITCODE. – not2qubit Dec 11 '22 at 06:40
  • 1
    @not2qubit, indeed, small, positive numbers are _typical_, and, for cross-platform use you're limited to positive numbers between `0` and `255`. Technically, however, process exit codes on Windows can be any signed 32-bit integer value (`[int]`/ `System.Int32`, in PowerShell / .NET terms). – mklement0 Dec 11 '22 at 14:24