From 2eb11c2f5d6f75eeb3b68b832f5b826b5e749499 Mon Sep 17 00:00:00 2001 From: Paul Marston Date: Fri, 22 May 2015 21:29:16 +0100 Subject: [PATCH 1/2] Adding try/catch logic to Write-VcsStatus Adding try/catch logic to Write-VcsStatus so that if one of the scriptblocks that has been added to the VcsPromptStatuses array fails the other scriptblocks are still invoked and the effect on the user's prompt is minimized Use of the ErrorVariable parameter during invocation is to also capture any errors that were written to the error stream --- GitPrompt.ps1 | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index 574685731..6f725e09e 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -37,6 +37,9 @@ $global:GitPromptSettings = New-Object PSObject -Property @{ UntrackedText = ' !' UntrackedForegroundColor = [ConsoleColor]::DarkRed UntrackedBackgroundColor = $Host.UI.RawUI.BackgroundColor + + ErrorForegroundColor = [ConsoleColor]::Red + ErrorBackgroundColor = $Host.UI.RawUI.BackgroundColor ShowStatusWhenZero = $true @@ -167,12 +170,32 @@ function Write-GitStatus($status) { if(!(Test-Path Variable:Global:VcsPromptStatuses)) { $Global:VcsPromptStatuses = @() } -function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } } + +function Global:Write-VcsStatus { + $Global:VcsPromptStatuses | foreach { + $vcsPromptErrors = $null + try { + Invoke-Command -ScriptBlock $_ -ErrorVariable vcsPromptErrors 2>$null + } + catch { + $vcsPromptErrors = $_ + } + if ($vcsPromptErrors.Length -gt 0) { + $vcsPromptErrors | % { Write-Error -ErrorRecord $_ -ErrorAction SilentlyContinue } + $s = $Global:GitPromptSettings + if ($s) { + Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor + Write-Prompt "Error" -BackgroundColor $s.ErrorBackgroundColor -ForegroundColor $s.ErrorForegroundColor + Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor + } + } + } +} # Add scriptblock that will execute for Write-VcsStatus $PoshGitVcsPrompt = { $Global:GitStatus = Get-GitStatus Write-GitStatus $GitStatus -} +} $Global:VcsPromptStatuses += $PoshGitVcsPrompt $ExecutionContext.SessionState.Module.OnRemove = { $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | ? { $_ -ne $PoshGitVcsPrompt} } \ No newline at end of file From a83182d6976a2beeb227ab9df3de1cc7c7e6c642 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Fri, 3 Mar 2017 01:59:38 -0600 Subject: [PATCH 2/2] Move try/catch logic to PoshGitVcsPrompt --- GitPrompt.ps1 | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index 6f725e09e..76245f61d 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -37,7 +37,7 @@ $global:GitPromptSettings = New-Object PSObject -Property @{ UntrackedText = ' !' UntrackedForegroundColor = [ConsoleColor]::DarkRed UntrackedBackgroundColor = $Host.UI.RawUI.BackgroundColor - + ErrorForegroundColor = [ConsoleColor]::Red ErrorBackgroundColor = $Host.UI.RawUI.BackgroundColor @@ -170,32 +170,22 @@ function Write-GitStatus($status) { if(!(Test-Path Variable:Global:VcsPromptStatuses)) { $Global:VcsPromptStatuses = @() } +function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } } -function Global:Write-VcsStatus { - $Global:VcsPromptStatuses | foreach { - $vcsPromptErrors = $null - try { - Invoke-Command -ScriptBlock $_ -ErrorVariable vcsPromptErrors 2>$null - } - catch { - $vcsPromptErrors = $_ - } - if ($vcsPromptErrors.Length -gt 0) { - $vcsPromptErrors | % { Write-Error -ErrorRecord $_ -ErrorAction SilentlyContinue } - $s = $Global:GitPromptSettings - if ($s) { - Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor - Write-Prompt "Error" -BackgroundColor $s.ErrorBackgroundColor -ForegroundColor $s.ErrorForegroundColor - Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor - } +# Add scriptblock that will execute for Write-VcsStatus +$PoshGitVcsPrompt = { + try { + $Global:GitStatus = Get-GitStatus + Write-GitStatus $GitStatus + } + catch { + $s = $Global:GitPromptSettings + if ($s) { + Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor + Write-Prompt "Error: $_" -BackgroundColor $s.ErrorBackgroundColor -ForegroundColor $s.ErrorForegroundColor + Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor } } } - -# Add scriptblock that will execute for Write-VcsStatus -$PoshGitVcsPrompt = { - $Global:GitStatus = Get-GitStatus - Write-GitStatus $GitStatus -} $Global:VcsPromptStatuses += $PoshGitVcsPrompt $ExecutionContext.SessionState.Module.OnRemove = { $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | ? { $_ -ne $PoshGitVcsPrompt} } \ No newline at end of file