Skip to content

Commit

Permalink
Merge pull request #277 from hmmwhatsthisdo/ErrorDetails_ErrorHandler…
Browse files Browse the repository at this point in the history
…Fix-276

Improve error-handler to accommodate for ErrorDetails and non-JSON/HTML responses
  • Loading branch information
lipkau authored Jun 2, 2018
2 parents 23ab6c6 + ad2f42a commit 6df414b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
7 changes: 6 additions & 1 deletion JiraPS/JiraPS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 24 additions & 4 deletions JiraPS/Public/Invoke-JiraMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,24 @@ 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 {
$webResponse = $webResponse.Exception.Response
}
}

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"

Expand All @@ -148,13 +156,25 @@ 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]*\<html\>") { $responseBody = '{"errorMessages": "Invalid server response. HTML returned."}' }

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 = [PSCustomObject]@{
errorMessages = @(
$responseBody
)
}
}

}

}
Expand All @@ -178,7 +198,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 {
Expand Down

0 comments on commit 6df414b

Please sign in to comment.