-
-
Save sassdawe/8921428e0920d053c299908ac972b20b to your computer and use it in GitHub Desktop.
Catch only specific errors coming from native commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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