From f2567150de3138256240feb6239db63955894acd Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 13 Nov 2020 10:57:38 -0600 Subject: [PATCH] Improves error handling and yml props * better bubbles up server-returned error messages to client * adds support for read_token in .cif.yml --- CIF3.psd1 | Bin 9414 -> 9414 bytes Private/Send-CIF3Api.ps1 | 9 ++++----- Public/Get-CIF3Config.ps1 | 10 ++++++++++ Public/Set-CIF3Config.ps1 | 8 ++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CIF3.psd1 b/CIF3.psd1 index 2317e5a44a4c95792bf2f6c3a7e355effde70b30..f72b9cd6bc38e9cdb2494fd225eb225cfa3a5a22 100644 GIT binary patch delta 18 acmX@+dCYUe6-GwW$qU)UH@{)Lr33&?`Ul4V delta 16 YcmX@+dCYUe6~@U;>=K*bFy2xE07I$=bN~PV diff --git a/Private/Send-CIF3Api.ps1 b/Private/Send-CIF3Api.ps1 index e41a153..6d19fa3 100644 --- a/Private/Send-CIF3Api.ps1 +++ b/Private/Send-CIF3Api.ps1 @@ -135,9 +135,6 @@ function Send-CIF3Api { Send-CIF3Api @PSBoundParameters } - elseif ($_.Exception.Response.StatusCode -eq 400) { - Write-Warning -Exception $_.Exception -Message 'Server returned 400. Invalid search?' - } elseif ($_.Exception.Response.StatusCode -eq 401) { Write-Error -Exception $_.Exception -Message 'Server returned 401 Unauthorized. Check your token?' } @@ -149,10 +146,12 @@ function Send-CIF3Api { } elseif ($null -ne $_.ErrorDetails.Message -and $_.ErrorDetails.Message -ne '') { # Convert the error-message to an object. (Invoke-RestMethod will not return data by-default if a 4xx/5xx status code is generated.) - $_.ErrorDetails.Message + $Message = $_.ErrorDetails.Message | ConvertFrom-Json | Select-Object -ExpandProperty 'message' + Write-Error -Exception $_.Exception -Message "Server returned $($_.Exception.Response.StatusCode.Value__). $($Message)" } else { - Write-Error -Exception $_.Exception -Message "CIFv3 API call failed: $_. Check remote Uri?" + Write-Error -Exception $_.Exception -Message "Server returned $($_.Exception.Response.StatusCode.Value__). + CIFv3 API call failed: $_. Check remote Uri?" } } diff --git a/Public/Get-CIF3Config.ps1 b/Public/Get-CIF3Config.ps1 index 578f6f0..c8c1545 100644 --- a/Public/Get-CIF3Config.ps1 +++ b/Public/Get-CIF3Config.ps1 @@ -51,9 +51,19 @@ function Get-CIF3Config { @{l = 'Proxy'; e = { $_.client.proxy } }, @{l = 'Uri'; e = { Decrypt $_.client.remote } }, @{l = 'Token'; e = { Decrypt $_.client.token } }, + @{l = 'ReadToken'; e = { Decrypt $_.client.read_token } }, @{l = 'ForceVerbose'; e = { $_.client.force_verbose } }, @{l = 'NoVerifySsl'; e = { $_.client.no_verify_ssl } } + # yml file can use 'token' or 'read_token' as name; if no 'token' property, look for 'read_token' + if ($null -eq $TempObj.Token -and $null -ne $TempObj.ReadToken) { + $TempObj.Token = $TempObj.ReadToken + } + # backfill ReadToken prop if it's empty + if ($null -ne $TempObj.Token -and $null -eq $TempObj.ReadToken) { + $TempObj.ReadToken = $TempObj.Token + } + # Nice oneliner to convert PSCustomObject to Hashtable: https://stackoverflow.com/questions/3740128/pscustomobject-to-hashtable $TempObj.PSObject.Properties | ForEach-Object -Begin { $h = @{ } } -Process { $h."$($_.Name)" = $_.Value } -End { $h } } diff --git a/Public/Set-CIF3Config.ps1 b/Public/Set-CIF3Config.ps1 index ce481d5..68c7e16 100644 --- a/Public/Set-CIF3Config.ps1 +++ b/Public/Set-CIF3Config.ps1 @@ -14,6 +14,9 @@ function Set-CIF3Config { .PARAMETER Token Specify a Token to use + .PARAMETER ReadToken + Specify a read Token to use (used if you have separate read/write tokens) + .PARAMETER EncryptToken If set to true, serializes token to disk via DPAPI (Windows only) @@ -46,6 +49,9 @@ function Set-CIF3Config { [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [string]$Token, + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] + [string]$ReadToken, + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [string]$Proxy, @@ -66,6 +72,7 @@ function Set-CIF3Config { switch ($PSBoundParameters.Keys) { 'Uri' { $Script:CIF3.Uri = $Uri } 'Token' { $Script:CIF3.Token = $Token } + 'ReadToken' { $Script:CIF3.ReadToken = $ReadToken } 'Proxy' { $Script:CIF3.Proxy = $Proxy } 'ForceVerbose' { $Script:CIF3.ForceVerbose = $ForceVerbose } 'NoVerifySsl' { $Script:CIF3.NoVerifySsl = $NoVerifySsl } @@ -86,6 +93,7 @@ function Set-CIF3Config { $OrderedCIFSettings.Add("client", [ordered]@{ "remote" = "$($Script:CIF3.Uri)" "token" = "$(Encrypt $Script:CIF3.Token)" + "read_token" = "$(Encrypt $Script:CIF3.ReadToken)" "no_verify_ssl" = "$($Script:CIF3.NoVerifySsl)" "force_verbose" = "$($Script:CIF3.ForceVerbose)" "proxy" = "$($Script:CIF3.Proxy)"