DocNougat / Meraki-Powershell-Module

A PowerShell Module for the Cisco Meraki API
GNU General Public License v3.0
18 stars 3 forks source link

try catch should throw exception #5

Closed abelal83 closed 9 months ago

abelal83 commented 9 months ago

First of all, thank you very much for creating and publishing this module, fantastic work and I hope to contribute as I've got a big Meraki project coming up.

I noticed as I was testing the module and errors were being encoutered my try catch which wraps the module functions were not catching errors. Of course, this is because the functions themselves are using try catch with the error being written to host. This unfortunately prevent scripts from handling the errors.

For example, when I do the below:

try {
   Invoke-MerakiNetworkClaimDevices -AuthToken $Appsettings.meraki.api -NetworkId $network.id -DeviceSerials $deviceSerials
}
catch {
   # do my own thing here to handle error
}

I get an error written to console

{
  "errors": [
    "Device with serial Q3AC-XXXX-XXXX is already claimed and in tst03 (network ID: L_XXXX)",
    "Device with serial Q3AC-XXXX-XXXX is already claimed and in tst03 (network ID: L_XXXX)"
  ]
}

What I'd prefer to do is catch the error and handle it myself.

The simplest solution is to remove all try catch code in module, or alternatively after writing to console/host throw $_ immediately after.

    catch {
        Write-Host $_
        throw $_
    }
DocNougat commented 9 months ago

Thank you for the feedback! It makes me really happy to know that other people are making use of this module. This suggestion makes total sense to me. I've gone ahead and updated the module to v1.0.6 and updated the catch statements to all use your throw $_ method.