Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created November 4, 2022 17:40
Show Gist options
  • Save JustinGrote/acb8f3e1aa7e6cbda222f12cdbc5c865 to your computer and use it in GitHub Desktop.
Save JustinGrote/acb8f3e1aa7e6cbda222f12cdbc5c865 to your computer and use it in GitHub Desktop.
Catch only specific errors coming from native commands
filter ThrowStdOutErrors($messageFilter,[Parameter(ValueFromPipeline)]$obj) {
if ($obj -is [Management.Automation.ErrorRecord]) {
if ($obj -match $messageFilter) {
throw $obj
} else {
Write-Error $obj
return
}
}
$obj
}
try {
git nomatch 2>&1 | ThrowStdOutErrors 'kab*'
git kablam 2>&1 | ThrowStdOutErrors 'kab*'
} catch {
'Caught kablam!'
}
#Will pass on the error from nomatch and will catch kablam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment