mirror of
https://github.com/snazy2000/SnipeitPS.git
synced 2025-12-13 18:02:30 +00:00
Wrapped json parsing with try/catch
This commit is contained in:
parent
5a10799eb8
commit
4810bbf54f
1 changed files with 22 additions and 16 deletions
|
|
@ -85,26 +85,32 @@
|
||||||
Write-Verbose $webResponse.Content
|
Write-Verbose $webResponse.Content
|
||||||
|
|
||||||
# API returned a Content: lets work wit it
|
# API returned a Content: lets work wit it
|
||||||
$response = ConvertFrom-Json -InputObject $webResponse.Content
|
try{
|
||||||
|
$response = ConvertFrom-Json -InputObject $webResponse.Content
|
||||||
|
|
||||||
if ($response.status -eq "error") {
|
if ($response.status -eq "error") {
|
||||||
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
|
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
|
||||||
# This could be handled nicely in an function such as:
|
# This could be handled nicely in an function such as:
|
||||||
# ResolveError $response -WriteError
|
# ResolveError $response -WriteError
|
||||||
Write-Error $($response.messages | Out-String)
|
Write-Error $($response.messages | Out-String)
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = $response
|
|
||||||
if (($response) -and ($response | Get-Member -Name payload))
|
|
||||||
{
|
|
||||||
$result = $response.payload
|
|
||||||
}
|
|
||||||
elseif (($response) -and ($response | Get-Member -Name rows)) {
|
|
||||||
$result = $response.rows
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$result = $response
|
||||||
|
if (($response) -and ($response | Get-Member -Name payload))
|
||||||
|
{
|
||||||
|
$result = $response.payload
|
||||||
|
}
|
||||||
|
elseif (($response) -and ($response | Get-Member -Name rows)) {
|
||||||
|
$result = $response.rows
|
||||||
|
}
|
||||||
|
|
||||||
$result
|
$result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch {
|
||||||
|
Write-Warning "Cannot parse server response. To debug try to add -Verbose with command."
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
elseif ($webResponse.StatusCode -eq "Unauthorized") {
|
elseif ($webResponse.StatusCode -eq "Unauthorized") {
|
||||||
Write-Error "[$($MyInvocation.MyCommand.Name)] You are not Authorized to access the resource, check your token is correct"
|
Write-Error "[$($MyInvocation.MyCommand.Name)] You are not Authorized to access the resource, check your token is correct"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue