Skip to content

Commit

Permalink
Detecting errors generated by write-error
Browse files Browse the repository at this point in the history
try {} catch {} only catches terminating thrown errors, using
invoke-command with the errorvariable captures the error stream
  • Loading branch information
paulmarsy committed May 18, 2015
1 parent bc476ff commit b6dd6ea
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ if(!(Test-Path Variable:Global:VcsPromptStatuses)) {
}

function Global:Write-VcsStatus {
$Global:VcsPromptStatuses | foreach {
$Global:VcsPromptStatuses | foreach {
$vcsPromptError = $null
try {
$_.Invoke()
Invoke-Command -ScriptBlock $_ -ErrorVariable vcsPromptError 2>$null
}
catch {
$vcsPromptError = $_
}
if ($null -ne $vcsPromptErrors) {
# Log the error but dont affect the users prompt, splat the thrown object onto Write-Error so the relevant properties are automatically assigned
Write-Error -ErrorAction SilentlyContinue @_
Write-Error -ErrorAction SilentlyContinue @vcsPromptError
$s = $Global:GitPromptSettings
if ($s) {
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor
Expand Down

0 comments on commit b6dd6ea

Please sign in to comment.