Skip to content

Commit

Permalink
Improves error handling and yml props
Browse files Browse the repository at this point in the history
* better bubbles up server-returned error messages to client
* adds support for read_token in .cif.yml
  • Loading branch information
mdavis332 authored and chodonne committed Nov 13, 2020
1 parent b959737 commit f256715
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Binary file modified CIF3.psd1
Binary file not shown.
9 changes: 4 additions & 5 deletions Private/Send-CIF3Api.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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?'
}
Expand All @@ -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?"
}
}

Expand Down
10 changes: 10 additions & 0 deletions Public/Get-CIF3Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
8 changes: 8 additions & 0 deletions Public/Set-CIF3Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,

Expand All @@ -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 }
Expand All @@ -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)"
Expand Down

0 comments on commit f256715

Please sign in to comment.