Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
84152a5
beginning
CarloToso Dec 20, 2022
c36b8e2
Better Xml Error
CarloToso Dec 21, 2022
4401247
remove extra space
CarloToso Dec 21, 2022
408248e
Add two alternatives for settings.encoding
CarloToso Dec 21, 2022
dc17e16
fix error
CarloToso Dec 21, 2022
b6ecfd5
e -> E
CarloToso Dec 21, 2022
a0157f8
FormatErrorMessage; add jsonerror
CarloToso Dec 21, 2022
fa867ef
remove empty line
CarloToso Dec 21, 2022
e3305db
fix codefactor
CarloToso Dec 21, 2022
20467ea
fix errors
CarloToso Dec 21, 2022
85e76c8
add new line at the beginning of xml and json errors
CarloToso Dec 22, 2022
a44d17a
add newline to html error; remove regex html xml
CarloToso Dec 23, 2022
1e4dca9
;
CarloToso Dec 23, 2022
0db9ca4
remove newline (broke tests)
CarloToso Dec 24, 2022
70a776a
move FormatErrorMessage in Helper Methods
CarloToso Dec 25, 2022
cc37269
Revert to old behaviour for plain text and html
CarloToso Jan 6, 2023
bf3d007
fix error try-catch
CarloToso Jan 29, 2023
a2482cd
remove tabs
CarloToso Jan 29, 2023
ce5ca66
Add back NewLines
CarloToso Jan 30, 2023
d0f88d4
add tests for xml and json errors
CarloToso Jan 30, 2023
28d8152
spacing
CarloToso Jan 30, 2023
f2992b3
spaces
CarloToso Jan 30, 2023
7618c2b
fix?
CarloToso Jan 30, 2023
7edeedb
fix param
CarloToso Jan 30, 2023
a5ccbfe
different newline if windows
CarloToso Jan 30, 2023
b3065c1
Merge branch 'master' into Invoke-WebRequest-fix-xml-error
CarloToso Feb 8, 2023
cc5a5c5
small changes Xml error
CarloToso Feb 8, 2023
7b04f41
ewmoved encoding the default is already UTF8
CarloToso Feb 8, 2023
c7ff97c
follow suggestions
CarloToso Feb 8, 2023
586583a
remove bool converted
CarloToso Feb 8, 2023
f2a2113
remove String.Length > 0
CarloToso Feb 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1463,10 +1465,8 @@ protected override void ProcessRecord()
StreamReader reader = null;
try
{
reader = new(StreamHelper.GetResponseStream(response));

// Remove HTML tags making it easier to read
detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty);
reader = new StreamReader(StreamHelper.GetResponseStream(response));
detailMsg = FormatErrorMessage(reader.ReadToEnd(), contentType);
}
catch
{
Expand Down Expand Up @@ -1821,6 +1821,60 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo

return result;
}

private static string FormatErrorMessage(string error, string contentType)
{
string formattedError = null;

try
{
if (ContentHelper.IsXml(contentType))
{
XmlDocument doc = new();
doc.LoadXml(error);

XmlWriterSettings settings = new XmlWriterSettings {
Indent = true,
NewLineOnAttributes = true,
OmitXmlDeclaration = true
};

if (doc.FirstChild is XmlDeclaration)
{
XmlDeclaration decl = doc.FirstChild as XmlDeclaration;
settings.Encoding = Encoding.GetEncoding(decl.Encoding);
}

StringBuilder stringBuilder = new();
using XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings);
doc.Save(xmlWriter);
string xmlString = stringBuilder.ToString();

formattedError = Environment.NewLine + xmlString;
}
else if (ContentHelper.IsJson(contentType))
{
JsonNode jsonNode = JsonNode.Parse(error);
JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = jsonNode.ToJsonString(options);

formattedError = Environment.NewLine + jsonString;
}
}
catch
{
// Ignore errors
}

if (string.IsNullOrEmpty(formattedError))
{
// Remove HTML tags making it easier to read
formattedError = System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty);
}

return formattedError;
}

#endregion Helper Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,35 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
}

It "Validate Invoke-WebRequest returns <type> errors in exception" -TestCases @(
@{ type = "XML"
query = @{
contenttype = 'application/xml'
body = '<?xml version="1.0" encoding="UTF-8"?><Error><Code>418</Code><Message>I am a teapot!!!</Message></Error>'
statuscode = 418
responsephrase = "I am a teapot"
}
expectederror = $IsWindows ? "`r`n<Error>`r`n <Code>418</Code>`r`n <Message>I am a teapot!!!</Message>`r`n</Error>" : "`n<Error>`n <Code>418</Code>`n <Message>I am a teapot!!!</Message>`n</Error>"
}

@{ type = "Json"
query = @{
contenttype = 'application/json'
body = '{"error":{"code":"418", "message":"I am a teapot!!!"}}'
statuscode = 418
responsephrase = "I am a teapot"
}
expectederror = $IsWindows ? "`r`n{`r`n `"error`": {`r`n `"code`": `"418`",`r`n `"message`": `"I am a teapot!!!`"`r`n }`r`n}" : "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}"
}
) {
param($query, $expectederror)
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$command = "Invoke-WebRequest -Uri '$uri'"
$result = ExecuteWebCommand -command $command

$result.Error.ErrorDetails.Message | Should -Be $expectederror
}

It "Validate Invoke-WebRequest returns empty RelationLink property if there is no Link Header" {
$uri = $uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri'"
Expand Down