Skip to content

Commit

Permalink
fixed error handling on public functions that were either throwing do…
Browse files Browse the repository at this point in the history
…uble errors in some cases or mis-attributing the function where the error occurred
  • Loading branch information
rmbolger committed Oct 17, 2022
1 parent da7e19c commit aa116b7
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 33 deletions.
1 change: 1 addition & 0 deletions Posh-IBWAPI/Private/HighestVer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function HighestVer
WAPIVersion = '1.1'
Credential = $Credential
SkipCertificateCheck = $SkipCertificateCheck.IsPresent
ErrorAction = 'Stop'
}
$versions = (Invoke-IBWAPI -Query '?_schema' @opts).supported_versions

Expand Down
38 changes: 24 additions & 14 deletions Posh-IBWAPI/Public/Get-IBObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ function Get-IBObject

# if we're not paging, just return the single call
if (-not $UsePaging) {
$query = '{0}?{1}' -f $queryObj,($queryargs -join '&')
return (Invoke-IBWAPI -Query $query @opts)
$query = ('{0}?{1}' -f $queryObj,($queryargs -join '&')).TrimEnd('?')
try {
Invoke-IBWAPI -Query $query @opts -EA Stop
} catch { $PSCmdlet.WriteError($_) }

return
}

# By default, the WAPI will return an error if the result count exceeds 1000
Expand Down Expand Up @@ -223,18 +227,22 @@ function Get-IBObject

$query = '{0}{1}' -f $queryObj,$querystring

$response = Invoke-IBWAPI -Query $query @opts
if ('result' -notin $response.PSObject.Properties.Name) {
# A normal response from WAPI will contain a 'result' object even
# if that object is empty because it couldn't find anything.
# But if there's no result object, something is wrong.
$PSCmdlet.ThrowTerminatingError([Management.Automation.ErrorRecord]::new(
"No 'result' object found in server response",
$null, [Management.Automation.ErrorCategory]::ObjectNotFound, $null
))
try {
$response = Invoke-IBWAPI -Query $query @opts -EA Stop
if ('result' -notin $response.PSObject.Properties.Name) {
# A normal response from WAPI will contain a 'result' object even
# if that object is empty because it couldn't find anything.
# But if there's no result object, something is wrong.
$PSCmdlet.ThrowTerminatingError([Management.Automation.ErrorRecord]::new(
"No 'result' object found in server response",
$null, [Management.Automation.ErrorCategory]::ObjectNotFound, $null
))
}
$resultCount += $response.result.Count
$response.result
} catch {
$PSCmdlet.WriteError($_)
}
$resultCount += $response.result.Count
$response.result

} while ($response.next_page_id -and $resultCount -lt $MaxResults)

Expand Down Expand Up @@ -282,7 +290,9 @@ function Get-IBObject
}
}

Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts
try {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts -EA Stop
} catch { $PSCmdlet.WriteError($_) }
}

}
Expand Down
12 changes: 9 additions & 3 deletions Posh-IBWAPI/Public/Get-IBSchema.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ function Get-IBSchema {

# make sure we can actually query schema stuff for this WAPIHost
if (-not $sCache.HighestVersion) {
$sCache.HighestVersion = (HighestVer @opts)
try {
$sCache.HighestVersion = (HighestVer @opts)
} catch { $PSCmdlet.ThrowTerminatingError($_) }
Write-Debug "Set highest version: $($sCache.HighestVersion)"
}
if ([Version]$sCache.HighestVersion -lt [Version]'1.7.5') {
Expand All @@ -49,7 +51,9 @@ function Get-IBSchema {

# cache some base schema stuff that we'll potentially need later
if (-not $sCache.SupportedVersions -or -not $sCache[$WAPIVersion]) {
$schema = Invoke-IBWAPI -Query '?_schema' @opts
try {
$schema = Invoke-IBWAPI -Query '?_schema' @opts -EA Stop
} catch { $PsCmdlet.ThrowTerminatingError($_) }

# set supported versions
$sCache.SupportedVersions = $schema.supported_versions | Sort-Object @{E={[Version]$_}}
Expand Down Expand Up @@ -134,7 +138,9 @@ function Get-IBSchema {
$query += "&_schema_version=2&_schema_searchable=1&_get_doc=1"
}

$schema = Invoke-IBWAPI -Query $query @opts
try {
$schema = Invoke-IBWAPI -Query $query @opts -EA Stop
} catch { $PsCmdlet.ThrowTerminatingError($_) }

# check for the switches that will prevent additional output
if ($Raw -or $LaunchHTML) {
Expand Down
6 changes: 5 additions & 1 deletion Posh-IBWAPI/Public/Invoke-IBFunction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ function Invoke-IBFunction
$queryParams = @{
Query = '{0}?_function={1}' -f $ObjectRef,$FunctionName
Method = 'POST'
ErrorAction = 'Stop'
}
if ($FunctionArgs) {
$queryParams.Body = $FunctionArgs
}

# make the call
if ($PSCmdlet.ShouldProcess($queryParams.Uri, "POST")) {
Invoke-IBWAPI @queryParams @opts
try {
Invoke-IBWAPI @queryParams @opts
} catch { $PsCmdlet.WriteError($_) }

}

}
Expand Down
9 changes: 7 additions & 2 deletions Posh-IBWAPI/Public/New-IBObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ function New-IBObject
Body = $IBObject
}
if ($PSCmdlet.ShouldProcess($queryParams.Uri, "POST")) {
Invoke-IBWAPI @queryParams @opts
try {
Invoke-IBWAPI @queryParams @opts -EA Stop
} catch { $PsCmdlet.WriteError($_) }

}
}

Expand Down Expand Up @@ -103,7 +106,9 @@ function New-IBObject
}

if ($PSCmdlet.ShouldProcess($opts.WAPIHost, 'POST')) {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts
try {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts -EA Stop
} catch { $PsCmdlet.WriteError($_) }
}
}

Expand Down
23 changes: 19 additions & 4 deletions Posh-IBWAPI/Public/Receive-IBFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ function Receive-IBFile {
Process {

# requestion the download token and url
$response = Invoke-IBFunction -ObjectRef $ObjectRef `
-FunctionName $FunctionName -FunctionArgs $FunctionArgs @opts -EA Stop
$funcParams = @{
ObjectRef = $ObjectRef
FunctionName = $FunctionName
FunctionArgs = $FunctionArgs
ErrorAction = 'Stop'
}
try {
$response = Invoke-IBFunction @funcParams @opts
} catch { $PsCmdlet.ThrowTerminatingError($_) }
$dlUrl = $response.url

# try to download the file
Expand Down Expand Up @@ -74,11 +81,19 @@ function Receive-IBFile {
Write-Debug "Downloading file"
Invoke-IBWAPI @restOpts
}
catch { $PSCmdlet.ThrowTerminatingError($_) }
finally {
# inform Infoblox that the download is complete
if ($response.token) {
$null = Invoke-IBFunction -ObjectRef 'fileop' `
-FunctionName 'downloadcomplete' -FunctionArgs @{token=$response.token} @opts
$funcParams = @{
ObjectRef = 'fileop'
FunctionName = 'downloadcomplete'
FunctionArgs = @{ token = $response.token }
ErrorAction = 'Stop'
}
try {
$null = Invoke-IBFunction @funcParams @opts
} catch { $PsCmdlet.ThrowTerminatingError($_) }
}
}

Expand Down
8 changes: 6 additions & 2 deletions Posh-IBWAPI/Public/Remove-IBObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ function Remove-IBObject

$query = '{0}{1}' -f $ObjectRef,$querystring
if ($PSCmdlet.ShouldProcess($opts.WAPIHOST, 'DELETE')) {
Invoke-IBWAPI -Query $query -Method Delete @opts
try {
Invoke-IBWAPI -Query $query -Method Delete @opts -EA Stop
} catch { $PsCmdlet.WriteError($_) }
}
}

Expand All @@ -70,7 +72,9 @@ function Remove-IBObject
}

if ($PSCmdlet.ShouldProcess($opts.WAPIHost, 'POST')) {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts
try {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts -EA Stop
} catch { $PsCmdlet.WriteError($_) }
}
}

Expand Down
19 changes: 14 additions & 5 deletions Posh-IBWAPI/Public/Send-IBFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ function Send-IBFile {
$Path = $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)

Write-Debug "Calling uploadinit"
$response = Invoke-IBFunction -ObjectRef 'fileop' -FunctionName 'uploadinit' @opts -EA Stop
$token = $response.token
$uploadUrl = $response.url
try {
$response = Invoke-IBFunction -ObjectRef 'fileop' -FunctionName 'uploadinit' @opts -EA Stop
$token = $response.token
$uploadUrl = $response.url
} catch { $PsCmdlet.ThrowTerminatingError($_) }

# while we'd love to use the built-in support for multipart/file uploads in Invoke-RestMethod, it's
# only available in PowerShell 6.1+ and the implementation currently has some bugs we'd need
Expand Down Expand Up @@ -99,8 +101,15 @@ function Send-IBFile {

# finalize the upload with the actual requested function and arguments
Write-Debug "Calling $FunctionName with associated arguments"
$response = Invoke-IBFunction -ObjectRef $ObjectRef -FunctionName $FunctionName `
-FunctionArgs $FunctionArgs @opts -EA Stop
$funcParams = @{
ObjectRef = $ObjectRef
FunctionName = $FunctionName
FunctionArgs = $FunctionArgs
ErrorAction = 'Stop'
}
try {
$response = Invoke-IBFunction @funcParams @opts
} catch { $PsCmdlet.ThrowTerminatingError($_) }

}
}
8 changes: 6 additions & 2 deletions Posh-IBWAPI/Public/Set-IBObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ function Set-IBObject

$query = '{0}{1}' -f $ObjectRef,$querystring
if ($PsCmdlet.ShouldProcess($opts.WAPIHost, 'PUT')) {
Invoke-IBWAPI -Query $query -Method 'PUT' -Body $TemplateObject @opts
try {
Invoke-IBWAPI -Query $query -Method 'PUT' -Body $TemplateObject @opts -EA Stop
} catch { $PsCmdlet.WriteError($_) }
}
}

Expand Down Expand Up @@ -144,7 +146,9 @@ function Set-IBObject
}

if ($PSCmdlet.ShouldProcess($opts.WAPIHost, 'POST')) {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts
try {
Invoke-IBWAPI -Query 'request' -Method 'POST' -Body $body @opts -EA Stop
} catch { $PsCmdlet.WriteError($_) }
}
}

Expand Down

0 comments on commit aa116b7

Please sign in to comment.