From a2d1a7bcec9267d6fdbea2f8f9c9d3e36675a1e2 Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 14:00:08 -0700 Subject: [PATCH 1/7] Fix misleading comment --- JiraPS/Public/Invoke-JiraMethod.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/JiraPS/Public/Invoke-JiraMethod.ps1 b/JiraPS/Public/Invoke-JiraMethod.ps1 index 2cecd952..5308e9c1 100644 --- a/JiraPS/Public/Invoke-JiraMethod.ps1 +++ b/JiraPS/Public/Invoke-JiraMethod.ps1 @@ -120,8 +120,10 @@ function Invoke-JiraMethod { # Invoke-WebRequest is hard-coded to throw an exception if the Web request returns a 4xx or 5xx error. # This is the best workaround I can find to retrieve the actual results of the request. $webResponse = $_ + # ErrorDetails behavior is erratic and may not always be available + # See https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/17142518--errordetails-is-null-when-invoke-webrequest-or + # PSv6+ appears to be unaffected if ($webResponse.ErrorDetails) { - # In PowerShellCore (v6+), the response body is available as string $responseBody = $webResponse.ErrorDetails.Message } else { From 248a372fa74e5ad9426c95fc0ad7563455bbe88d Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 14:01:18 -0700 Subject: [PATCH 2/7] Split error handler if-block to account for $responseBody being pre-set --- JiraPS/Public/Invoke-JiraMethod.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/JiraPS/Public/Invoke-JiraMethod.ps1 b/JiraPS/Public/Invoke-JiraMethod.ps1 index 5308e9c1..f0e33c4e 100644 --- a/JiraPS/Public/Invoke-JiraMethod.ps1 +++ b/JiraPS/Public/Invoke-JiraMethod.ps1 @@ -150,7 +150,9 @@ function Invoke-JiraMethod { $readStream = New-Object -TypeName System.IO.StreamReader -ArgumentList ($webResponse.GetResponseStream()) $responseBody = $readStream.ReadToEnd() $readStream.Close() + } + If ($responseBody) { # Clear the body in case it is not a JSON (but rather html) if ($responseBody -match "^[\s\t]*\") { $responseBody = '{"errorMessages": "Invalid server response. HTML returned."}' } From 9cc4ca7b346bbb63c82ed92ea39df473f32431b8 Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 14:05:09 -0700 Subject: [PATCH 3/7] Added handler for API responses that are neither JSON or HTML --- JiraPS/Public/Invoke-JiraMethod.ps1 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/JiraPS/Public/Invoke-JiraMethod.ps1 b/JiraPS/Public/Invoke-JiraMethod.ps1 index f0e33c4e..c7dc9048 100644 --- a/JiraPS/Public/Invoke-JiraMethod.ps1 +++ b/JiraPS/Public/Invoke-JiraMethod.ps1 @@ -158,7 +158,18 @@ function Invoke-JiraMethod { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Retrieved body of HTTP response for more information about the error (`$responseBody)" Write-Debug "[$($MyInvocation.MyCommand.Name)] Got the following error as `$responseBody" - $result = ConvertFrom-Json -InputObject $responseBody + try { + $result = ConvertFrom-Json -InputObject $responseBody + + } catch [ArgumentException] { # handle $responseBody being neither JSON nor HTML + $result = @{ + errorMessages = @( + "Non-JSON/HTML response returned from JIRA API" + $responseBody + ) + } + } + } } From 04bc6207a76bdae919068498d1f8153e6ea742f5 Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 14:09:15 -0700 Subject: [PATCH 4/7] Mark errorMessages as error-state members in result interpreter --- JiraPS/Public/Invoke-JiraMethod.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JiraPS/Public/Invoke-JiraMethod.ps1 b/JiraPS/Public/Invoke-JiraMethod.ps1 index c7dc9048..76774da1 100644 --- a/JiraPS/Public/Invoke-JiraMethod.ps1 +++ b/JiraPS/Public/Invoke-JiraMethod.ps1 @@ -193,7 +193,7 @@ function Invoke-JiraMethod { } if ($result) { - if (Get-Member -Name "Errors" -InputObject $result -ErrorAction SilentlyContinue) { + if (Get-Member -Name "Errors","errorMessages" -InputObject $result -ErrorAction SilentlyContinue) { Resolve-JiraError $result -WriteError -Cmdlet $Cmdlet } else { From 833c277c93cfdb60a09c3a8e9e4264efc52f77b7 Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 14:14:06 -0700 Subject: [PATCH 5/7] Remove unnecessary line from error-handler --- JiraPS/Public/Invoke-JiraMethod.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/JiraPS/Public/Invoke-JiraMethod.ps1 b/JiraPS/Public/Invoke-JiraMethod.ps1 index 76774da1..e4535752 100644 --- a/JiraPS/Public/Invoke-JiraMethod.ps1 +++ b/JiraPS/Public/Invoke-JiraMethod.ps1 @@ -162,9 +162,8 @@ function Invoke-JiraMethod { $result = ConvertFrom-Json -InputObject $responseBody } catch [ArgumentException] { # handle $responseBody being neither JSON nor HTML - $result = @{ + $result = [PSCustomObject]@{ errorMessages = @( - "Non-JSON/HTML response returned from JIRA API" $responseBody ) } From 5068109a8f521aa219aa8339668526109adb51c3 Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 15:05:36 -0700 Subject: [PATCH 6/7] Fix Test-ServerResponse call to handle exceptions with ErrorDetails --- JiraPS/Public/Invoke-JiraMethod.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/JiraPS/Public/Invoke-JiraMethod.ps1 b/JiraPS/Public/Invoke-JiraMethod.ps1 index e4535752..59deba97 100644 --- a/JiraPS/Public/Invoke-JiraMethod.ps1 +++ b/JiraPS/Public/Invoke-JiraMethod.ps1 @@ -131,7 +131,13 @@ function Invoke-JiraMethod { } } - Test-ServerResponse -InputObject $webResponse -Cmdlet $Cmdlet + if ($WebResponse.ErrorDetails) { + Test-ServerResponse -InputObject $webResponse.Exception.Response -Cmdlet $Cmdlet + } Else { + Test-ServerResponse -InputObject $webResponse -Cmdlet $Cmdlet + } + + Write-Debug "[$($MyInvocation.MyCommand.Name)] Executed WebRequest. Access `$webResponse to see details" From dfbe1317bcab44c29fd1e42f4b213963b372ca51 Mon Sep 17 00:00:00 2001 From: "Sean R. Williams" Date: Thu, 31 May 2018 15:09:19 -0700 Subject: [PATCH 7/7] Ensure System.Net.Http assembly is loaded when importing (as Test-ServerResponse requires it) --- JiraPS/JiraPS.psm1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/JiraPS/JiraPS.psm1 b/JiraPS/JiraPS.psm1 index c251e311..0b11a9ac 100644 --- a/JiraPS/JiraPS.psm1 +++ b/JiraPS/JiraPS.psm1 @@ -10,7 +10,12 @@ # Load Web assembly when needed # PowerShell Core has the assembly preloaded if (!("System.Web.HttpUtility" -as [Type])) { - Add-Type -Assembly System.Web + Add-Type -AssemblyName "System.Web" +} +# Load System.Net.Http when needed +# PowerShell Core has the assembly preloaded +if (!("System.Net.Http.HttpRequestException" -as [Type])) { + Add-Type -AssemblyName "System.Net.Http" } #endregion Dependencies