Powershell: Invoke-RestMethod does not respect error action preference.

Created on 20 Aug 2017  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Invoke-RestMethod -ErrorAction SilentlyContinue -ErrorVariable RestError -Uri http://nowhere

Expected behavior

Error suppressed for custom error handling. RestError variable is created and error inserted into collection.

Actual behavior

Error thrown in current runspace and passed to RestError

Environment data

> $PSVersionTable
Name                           Value                                                                                                                   
----                           -----                                                                                                                   
PSVersion                      6.0.0-beta                                                                                                              
PSEdition                      Core                                                                                                                    
GitCommitId                    v6.0.0-beta.5                                                                                                           
OS                             Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64          
Platform                       Unix                                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                 
PSRemotingProtocolVersion      2.3                                                                                                                     
SerializationVersion           1.1.0.1                                                                                                                 
WSManStackVersion              3.0                                
Area-Cmdlets-Utility Resolution-Answered

Most helpful comment

Thanks. I remember now that I've come across this before, but was clearly blanking on the terminating errors piece.

All 4 comments

I repro'd this on OS X as I'm working on cross-platform support for a project using PowerShell, but this same behavior also occurs on Windows 10, PowerShell v5.1.

The error you're provoking is a statement-_terminating_ one, whereas the common -ErrorAction parameter only applies to _non-terminating_ errors.

To catch a _terminating_ error, you must use either a try / catch or a trap statement.

This dichotomy between non-terminating and terminating errors has always existed, but was never explained well; see Our Error Handling, Ourselves - time to fully understand and properly document PowerShell's error handling.

P.S: What you're looking for is:

$RestError = $null
Try {
  Invoke-RestMethod -Uri http://nowhere
} Catch {
  $RestError = $_
}

Thanks. I remember now that I've come across this before, but was clearly blanking on the terminating errors piece.

Was this page helpful?
0 / 5 - 0 ratings