evergreen icon indicating copy to clipboard operation
evergreen copied to clipboard

[Bug]: Can't try/catch when a WARNING is displayed

Open BornToBeRoot opened this issue 2 years ago • 5 comments

What happened?

Hi @aaronparker,

when i use try/catch around the Get-EvergreenApp and an error/warning occours... the error is not catched. It also breaks my foreach loop...

For example if i do something like this:

foreach($app in $apps) {
  try { Get-EvergreenApp $app} catch {"catch!"}
}

image

Any idea or workaround?

Version

2202.525.0 & 2111.488

What PowerShell edition/s are you running Evergreen on?

Windows PowerShell

Which operating system/s are you running Evergreen on?

Windows 10, Other

Have you reviewed the documentation?

  • [x] Troubleshooting at: https://stealthpuppy.com/evergreen/troubleshoot/
  • [X] Known issues at: https://stealthpuppy.com/evergreen/issues/

Verbose output

VERBOSE: Get-EvergreenApp: Function exists: C:\Program
Files\WindowsPowerShell\Modules\Evergreen\2111.488\Apps\Get-CitrixWorkspaceApp.ps1.
VERBOSE: Get-EvergreenApp: Dot sourcing: C:\Program
Files\WindowsPowerShell\Modules\Evergreen\2111.488\Apps\Get-CitrixWorkspaceApp.ps1.
VERBOSE: Get-FunctionResource: read application resource strings from [C:\Program
Files\WindowsPowerShell\Modules\Evergreen\2111.488\Manifests\CitrixWorkspaceApp.json]
VERBOSE: Get-EvergreenApp: Calling: Get-CitrixWorkspaceApp.
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [UseBasicParsing: True].
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [MaximumRedirection: 1].
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [DisableKeepAlive: True].
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [UserAgent: CitrixReceiver/19.7.0.15
WinOS/10.0.18362].
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [Method: Default].
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [Uri:
https://downloadplugins.citrix.com/ReceiverUpdates/Prod/catalog_win.xml].
VERBOSE: Invoke-RestMethodWrapper: Invoke-RestMethod parameter: [ContentType: application/json; charset=utf-8].
VERBOSE: GET https://downloadplugins.citrix.com/ReceiverUpdates/Prod/catalog_win.xml with 0-byte payload
WARNING: Invoke-RestMethodWrapper: Error at URI:
https://downloadplugins.citrix.com/ReceiverUpdates/Prod/catalog_win.xml.
WARNING: Invoke-RestMethodWrapper: Error encountered: The remote server returned an error: (404) Not Found..
WARNING: Invoke-RestMethodWrapper: For troubleshooting steps see:
https://stealthpuppy.com/evergreen/troubleshoot/.

BornToBeRoot avatar Mar 28 '22 16:03 BornToBeRoot

I've been thinking about what should be returned - should we throw when encountering an issue, or display a warning and move onto the next download so that we don't stop the function if we have an issue with only one download?

For your example with CitrixWorkspaceApp, it's using Invoke-RestMethodWrapper, that includes the following which needs to be updated:

    try {
        $Response = Invoke-RestMethod @irmParams
    }
    catch {
        Write-Warning -Message "$($MyInvocation.MyCommand): Error at URI: $Uri."
        Write-Warning -Message "$($MyInvocation.MyCommand): Error encountered: $($_.Exception.Message)."
        Write-Warning -Message "$($MyInvocation.MyCommand): For troubleshooting steps see: $($script:resourceStrings.Uri.Info)."
        Break
        #Throw "$($MyInvocation.MyCommand): $($_.Exception.Message)."
    }

aaronparker avatar Mar 28 '22 21:03 aaronparker

I think it would make sense to throw an error (like any other powershell function) and then handle it with -ErrorAction or try/catch

I've looked into the function and i don't understand why you use a break here. There is no loop in this function you want to exit.

If you don't want to throw an error, you could do something like return or return $null to exit the function.

break --> Exit for, foreach, do, while-loop return --> Exit function and go back to the shell / continue exit --> Exit script/session/window


If you want to throw an error you can use throw for terminating error or Write-Error for non terminating error.

BornToBeRoot avatar Mar 29 '22 08:03 BornToBeRoot

I think the consideration here, is what's the best approach when using Evergreen in an image build or packaging factory pipeline. Perhaps it's best to leave error catching to who ever is using Evergreen in their scripts.

I've an update to the development that puts Throw back to the internal functions and I still need to review some of the application functions.

aaronparker avatar Mar 29 '22 09:03 aaronparker

Thanks! I will test it.

For me it broke my ps script in azure automation runbooks to check for software updats :)

BornToBeRoot avatar Mar 29 '22 09:03 BornToBeRoot

Pushed another update to development that should now support the following, allowing the function to continue even in the event of an error.

Get-EvergreenApp -Name MozillaFirefox -ErrorAction "Ignore" -WarningAction "Ignore"

aaronparker avatar Apr 01 '22 00:04 aaronparker