From df0162b90c15d8fbfaa089fe52ea09fc57fc2830 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 16 Mar 2017 17:59:15 -0700 Subject: [PATCH] put innerexception message as error detail rather than user getting generic error message (#3330) --- .../CoreCLR/WebRequestPSCmdlet.CoreClr.cs | 4 ++++ .../WebCmdlets.Tests.ps1 | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs index a1069f6c4..3699a991c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs @@ -445,6 +445,10 @@ namespace Microsoft.PowerShell.Commands catch (HttpRequestException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); + if (ex.InnerException != null) + { + er.ErrorDetails = new ErrorDetails(ex.InnerException.Message); + } ThrowTerminatingError(er); } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 07e65489f..d2d562ae2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -441,6 +441,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $result.Error.Exception.Message | Should Match ": 418 \(I'm a teapot\)\." $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } + + It "Validate Invoke-WebRequest returns native HTTPS error message in exception" { + + $command = "Invoke-WebRequest -Uri https://incomplete.chain.badssl.com" + $result = ExecuteWebCommand -command $command + + # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same + $result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message + $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + } } Describe "Invoke-RestMethod tests" -Tags "Feature" { @@ -725,6 +735,16 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result.Error.Exception.Message | Should Match ": 418 \(I'm a teapot\)\." $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } + + It "Validate Invoke-RestMethod returns native HTTPS error message in exception" { + + $command = "Invoke-RestMethod -Uri https://incomplete.chain.badssl.com" + $result = ExecuteWebCommand -command $command + + # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same + $result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message + $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + } } Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" {