From 4e992d48d144b376776c6868ea9a3d19aec3206b Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 9 Aug 2022 06:54:55 +0200 Subject: [PATCH] fix(pwsh): set default value for ErrorCode resolves #2633 --- src/shell/scripts/omp.ps1 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index b4f50510..4db68517 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -286,27 +286,34 @@ Example: } function Update-PoshErrorCode { - $script:ExecutionTime = -1 $lastHistory = Get-History -ErrorAction Ignore -Count 1 + + # default values + $script:ErrorCode = 0 + $script:ExecutionTime = -1 + # error code should be updated only when a non-empty command is run if (($null -eq $lastHistory) -or ($script:LastHistoryId -eq $lastHistory.Id)) { return } + $script:LastHistoryId = $lastHistory.Id $script:ExecutionTime = ($lastHistory.EndExecutionTime - $lastHistory.StartExecutionTime).TotalMilliseconds if ($script:OriginalLastExecutionStatus) { - $script:ErrorCode = 0 return } + $invocationInfo = try { # retrieve info of the most recent error $global:Error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo } catch { $null } + # check if the last command caused the last error if ($null -ne $invocationInfo -and $lastHistory.CommandLine -eq $invocationInfo.Line) { $script:ErrorCode = 1 return } + if ($script:OriginalLastExitCode -is [int] -and $script:OriginalLastExitCode -ne 0) { # native app exit code $script:ErrorCode = $script:OriginalLastExitCode