From 84152a54b1310134877503406af2f876f3a7a2d0 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 20 Dec 2022 21:11:06 +0100 Subject: [PATCH 01/30] beginning --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index dbbe169ae3d..c39dbbe92ed 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1535,8 +1535,15 @@ WebSession.Headers is not null && try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); - // remove HTML tags making it easier to read - detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); + if (ContentHelper.IsXml()) + { + + } + else + { + // remove HTML tags making it easier to read + detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); + } } catch (Exception) { From c36b8e225b1b5aa8f378b7a0683d68036034655c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 14:40:26 +0100 Subject: [PATCH 02/30] Better Xml Error --- .../Common/WebRequestPSCmdlet.Common.cs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index c39dbbe92ed..ebc0fb277b0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1533,11 +1533,13 @@ WebSession.Headers is not null && string detailMsg = string.Empty; StreamReader reader = null; try - { + { reader = new StreamReader(StreamHelper.GetResponseStream(response)); - if (ContentHelper.IsXml()) + if (ContentHelper.IsXml(contentType)) { - + string Error = XmlError(reader.ReadToEnd()); + Error = System.Text.RegularExpressions.Regex.Replace(Error, "", string.Empty); + detailMsg = System.Text.RegularExpressions.Regex.Replace(Error, "<(.*)>", "$1: "); } else { @@ -1934,5 +1936,23 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo return result; } #endregion Helper Methods + + private static String XmlError(string xml) + { + StringBuilder stringBuilder = new StringBuilder(); + XmlDocument doc = new XmlDocument(); + doc.LoadXml(xml); + + XmlWriterSettings settings = new XmlWriterSettings(); + //settings.Encoding = ; + settings.Indent = true; + settings.NewLineOnAttributes = true; + settings.OmitXmlDeclaration = true; + + XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); + doc.Save(xmlWriter); + + return stringBuilder.ToString(); + } } } From 44012476590353ac2d4c93c16df4f0ad21488d4b Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 14:56:49 +0100 Subject: [PATCH 03/30] remove extra space --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index ebc0fb277b0..abe8ba0085d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1533,7 +1533,7 @@ WebSession.Headers is not null && string detailMsg = string.Empty; StreamReader reader = null; try - { + { reader = new StreamReader(StreamHelper.GetResponseStream(response)); if (ContentHelper.IsXml(contentType)) { From 408248e242e535d577814592df7ce2b15baf0130 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:16:33 +0100 Subject: [PATCH 04/30] Add two alternatives for settings.encoding --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index abe8ba0085d..17aaefbd769 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1944,7 +1944,13 @@ private static String XmlError(string xml) doc.LoadXml(xml); XmlWriterSettings settings = new XmlWriterSettings(); - //settings.Encoding = ; + + settings.encoding = ContentHelper.GetDefaultEncoding(); + if (doc.FirstChild is XmlDeclaration) + { + settings.encoding = Encoding.GetEncoding(doc.FirstChild.Encoding); + } + settings.Encoding = doc.FirstChild is XmlDeclaration ? Encoding.GetEncoding(doc.FirstChild.Encoding) : ContentHelper.GetDefaultEncoding(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.OmitXmlDeclaration = true; From dc17e16da6d67f018d659030177a4d5f78dcb0b5 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:25:25 +0100 Subject: [PATCH 05/30] fix error --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 17aaefbd769..bd390528250 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1948,9 +1948,9 @@ private static String XmlError(string xml) settings.encoding = ContentHelper.GetDefaultEncoding(); if (doc.FirstChild is XmlDeclaration) { - settings.encoding = Encoding.GetEncoding(doc.FirstChild.Encoding); + XmlDeclaration decl = doc.FirstChild as XmlDeclaration; + settings.encoding = Encoding.GetEncoding(decl.Encoding); } - settings.Encoding = doc.FirstChild is XmlDeclaration ? Encoding.GetEncoding(doc.FirstChild.Encoding) : ContentHelper.GetDefaultEncoding(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.OmitXmlDeclaration = true; From b6ecfd59d41324532665b1b13e822e3407ca4611 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:36:50 +0100 Subject: [PATCH 06/30] e -> E --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index bd390528250..98ecc6ab4fa 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1945,11 +1945,11 @@ private static String XmlError(string xml) XmlWriterSettings settings = new XmlWriterSettings(); - settings.encoding = ContentHelper.GetDefaultEncoding(); + settings.Encoding = ContentHelper.GetDefaultEncoding(); if (doc.FirstChild is XmlDeclaration) { XmlDeclaration decl = doc.FirstChild as XmlDeclaration; - settings.encoding = Encoding.GetEncoding(decl.Encoding); + settings.Encoding = Encoding.GetEncoding(decl.Encoding); } settings.Indent = true; settings.NewLineOnAttributes = true; From a0157f808e152aafa98df5c1ddac9b32995f5964 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:45:19 +0100 Subject: [PATCH 07/30] FormatErrorMessage; add jsonerror --- .../Common/WebRequestPSCmdlet.Common.cs | 63 +++++++++++-------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 98ecc6ab4fa..9ecf32aedec 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -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; @@ -1535,17 +1537,7 @@ WebSession.Headers is not null && try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); - if (ContentHelper.IsXml(contentType)) - { - string Error = XmlError(reader.ReadToEnd()); - Error = System.Text.RegularExpressions.Regex.Replace(Error, "", string.Empty); - detailMsg = System.Text.RegularExpressions.Regex.Replace(Error, "<(.*)>", "$1: "); - } - else - { - // remove HTML tags making it easier to read - detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); - } + detailMsg = new FormatErrorMessage(reader.ReadToEnd(), contentType); } catch (Exception) { @@ -1937,28 +1929,45 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo } #endregion Helper Methods - private static String XmlError(string xml) + private static string FormatErrorMessage(string error, string contentType) { - StringBuilder stringBuilder = new StringBuilder(); - XmlDocument doc = new XmlDocument(); - doc.LoadXml(xml); + if (ContentHelper.IsXml(contentType)) + { + StringBuilder stringBuilder = new StringBuilder(); + XmlDocument doc = new XmlDocument(); + doc.LoadXml(error); + + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Indent = true; + settings.NewLineOnAttributes = true; + settings.OmitXmlDeclaration = true; + settings.Encoding = ContentHelper.GetDefaultEncoding(); + if (doc.FirstChild is XmlDeclaration) + { + XmlDeclaration decl = doc.FirstChild as XmlDeclaration; + settings.Encoding = Encoding.GetEncoding(decl.Encoding); + } + + XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); + doc.Save(xmlWriter); - XmlWriterSettings settings = new XmlWriterSettings(); + string xmlError = System.Text.RegularExpressions.Regex.Replace(stringBuilder.ToString(), "", string.Empty); + return System.Text.RegularExpressions.Regex.Replace(xmlError, "<(.*)>", "$1: "); - settings.Encoding = ContentHelper.GetDefaultEncoding(); - if (doc.FirstChild is XmlDeclaration) + } + else if (ContentHelper.IsJson(contentType)) { - XmlDeclaration decl = doc.FirstChild as XmlDeclaration; - settings.Encoding = Encoding.GetEncoding(decl.Encoding); + JsonNode jsonNode = JsonNode.Parse(error); + JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; + + return jsonNode.ToJsonString(options); + } + else + { + // remove HTML tags making it easier to read + return System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); } - settings.Indent = true; - settings.NewLineOnAttributes = true; - settings.OmitXmlDeclaration = true; - - XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); - doc.Save(xmlWriter); - return stringBuilder.ToString(); } } } From fa867ef6260d9deb80832ac2bcbbb9982d43365c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:46:12 +0100 Subject: [PATCH 08/30] remove empty line --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 9ecf32aedec..65754c41fdb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1967,7 +1967,6 @@ private static string FormatErrorMessage(string error, string contentType) // remove HTML tags making it easier to read return System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); } - } } } From e3305db84d0dbaa9028f300cac89e36edb3f1c03 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:48:03 +0100 Subject: [PATCH 09/30] fix codefactor --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 65754c41fdb..e1b4006ce57 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1953,13 +1953,12 @@ private static string FormatErrorMessage(string error, string contentType) string xmlError = System.Text.RegularExpressions.Regex.Replace(stringBuilder.ToString(), "", string.Empty); return System.Text.RegularExpressions.Regex.Replace(xmlError, "<(.*)>", "$1: "); - } else if (ContentHelper.IsJson(contentType)) { JsonNode jsonNode = JsonNode.Parse(error); - JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; - + JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; + return jsonNode.ToJsonString(options); } else From 20467eaf0bc52c5a678dcc3905bb536c102c1f92 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:57:09 +0100 Subject: [PATCH 10/30] fix errors --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index e1b4006ce57..69e19ace339 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1537,7 +1537,7 @@ WebSession.Headers is not null && try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); - detailMsg = new FormatErrorMessage(reader.ReadToEnd(), contentType); + detailMsg = FormatErrorMessage(reader.ReadToEnd(), contentType); } catch (Exception) { @@ -1964,7 +1964,7 @@ private static string FormatErrorMessage(string error, string contentType) else { // remove HTML tags making it easier to read - return System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); + return System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty); } } } From 85e76c8a252155a3496f46506f4a94ee5dbe609e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 22 Dec 2022 13:02:17 +0100 Subject: [PATCH 11/30] add new line at the beginning of xml and json errors --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 69e19ace339..1e7afbb421b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1952,14 +1952,14 @@ private static string FormatErrorMessage(string error, string contentType) doc.Save(xmlWriter); string xmlError = System.Text.RegularExpressions.Regex.Replace(stringBuilder.ToString(), "", string.Empty); - return System.Text.RegularExpressions.Regex.Replace(xmlError, "<(.*)>", "$1: "); + return Environment.NewLine + System.Text.RegularExpressions.Regex.Replace(xmlError, "<(.*)>", "$1: "); } else if (ContentHelper.IsJson(contentType)) { JsonNode jsonNode = JsonNode.Parse(error); JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; - return jsonNode.ToJsonString(options); + return Environment.NewLine + jsonNode.ToJsonString(options); } else { From a44d17a9d9a466f8a98632bb00f5c11637fe29ef Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 24 Dec 2022 00:01:22 +0100 Subject: [PATCH 12/30] add newline to html error; remove regex html xml --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 1e7afbb421b..fb1a640a2fb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1951,8 +1951,7 @@ private static string FormatErrorMessage(string error, string contentType) XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); doc.Save(xmlWriter); - string xmlError = System.Text.RegularExpressions.Regex.Replace(stringBuilder.ToString(), "", string.Empty); - return Environment.NewLine + System.Text.RegularExpressions.Regex.Replace(xmlError, "<(.*)>", "$1: "); + return Environment.NewLine + stringBuilder.ToString() } else if (ContentHelper.IsJson(contentType)) { @@ -1963,8 +1962,7 @@ private static string FormatErrorMessage(string error, string contentType) } else { - // remove HTML tags making it easier to read - return System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty); + return Environment.NewLine + error; } } } From 1e4dca9b7cdc2962101a409c6b1fc312c94c9d62 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 24 Dec 2022 00:12:45 +0100 Subject: [PATCH 13/30] ; --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index fb1a640a2fb..2e02a125e5e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1951,7 +1951,7 @@ private static string FormatErrorMessage(string error, string contentType) XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); doc.Save(xmlWriter); - return Environment.NewLine + stringBuilder.ToString() + return Environment.NewLine + stringBuilder.ToString(); } else if (ContentHelper.IsJson(contentType)) { From 0db9ca443b39aa9d2a9840b2cebb07253735d91b Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 24 Dec 2022 01:44:13 +0100 Subject: [PATCH 14/30] remove newline (broke tests) --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 2e02a125e5e..2dfd05fd5c7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1962,7 +1962,7 @@ private static string FormatErrorMessage(string error, string contentType) } else { - return Environment.NewLine + error; + return error; } } } From 70a776a6c18f1b12169cdc9ce96d0ea5df0c7d6d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 26 Dec 2022 00:18:12 +0100 Subject: [PATCH 15/30] move FormatErrorMessage in Helper Methods --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 2dfd05fd5c7..8a16c99526d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1927,7 +1927,6 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo return result; } - #endregion Helper Methods private static string FormatErrorMessage(string error, string contentType) { @@ -1965,5 +1964,6 @@ private static string FormatErrorMessage(string error, string contentType) return error; } } + #endregion Helper Methods } } From cc372695ba6b774bb03673668059e30b79c8420a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 6 Jan 2023 21:51:58 +0100 Subject: [PATCH 16/30] Revert to old behaviour for plain text and html --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 8a16c99526d..0508fa48be5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1961,7 +1961,8 @@ private static string FormatErrorMessage(string error, string contentType) } else { - return error; + // Remove HTML tags making it easier to read + return System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty); } } #endregion Helper Methods From bf3d007d7c56e7b8321cc940dd8a7f7372ef05af Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 00:41:59 +0100 Subject: [PATCH 17/30] fix error try-catch --- .../Common/WebRequestPSCmdlet.Common.cs | 74 ++++++++++++------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 0508fa48be5..895335b8ae0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1930,41 +1930,65 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo private static string FormatErrorMessage(string error, string contentType) { - if (ContentHelper.IsXml(contentType)) - { - StringBuilder stringBuilder = new StringBuilder(); - XmlDocument doc = new XmlDocument(); - doc.LoadXml(error); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Indent = true; - settings.NewLineOnAttributes = true; - settings.OmitXmlDeclaration = true; - settings.Encoding = ContentHelper.GetDefaultEncoding(); - if (doc.FirstChild is XmlDeclaration) + bool converted = false; + string formattedError = null; + + try + { + if (ContentHelper.IsXml(contentType)) { - XmlDeclaration decl = doc.FirstChild as XmlDeclaration; - settings.Encoding = Encoding.GetEncoding(decl.Encoding); - } + StringBuilder stringBuilder = new StringBuilder(); + XmlDocument doc = new XmlDocument(); + doc.LoadXml(error); + + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Indent = true; + settings.NewLineOnAttributes = true; + settings.OmitXmlDeclaration = true; + settings.Encoding = ContentHelper.GetDefaultEncoding(); + if (doc.FirstChild is XmlDeclaration) + { + XmlDeclaration decl = doc.FirstChild as XmlDeclaration; + settings.Encoding = Encoding.GetEncoding(decl.Encoding); + } - XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); - doc.Save(xmlWriter); + XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); + doc.Save(xmlWriter); + string xmlString = stringBuilder.ToString(); - return Environment.NewLine + stringBuilder.ToString(); + if (xmlString.Length > 0) + { + converted = true; + formattedError = xmlString; + } + } + else if (ContentHelper.IsJson(contentType)) + { + JsonNode jsonNode = JsonNode.Parse(error); + JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; + string jsonString = jsonNode.ToJsonString(options); + + if (jsonString.Length > 0) + { + converted = true; + formattedError = jsonString; + } + } } - else if (ContentHelper.IsJson(contentType)) + catch { - JsonNode jsonNode = JsonNode.Parse(error); - JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; - - return Environment.NewLine + jsonNode.ToJsonString(options); + // Ignore errors } - else + + if (converted is false) { // Remove HTML tags making it easier to read - return System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty); + formattedError = System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty); } + + return formattedError; } + #endregion Helper Methods } } From a2482cd8a19524297937fb8321e3758eff1a73a6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 00:44:40 +0100 Subject: [PATCH 18/30] remove tabs --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 895335b8ae0..77f0f0d3f0a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1964,7 +1964,7 @@ private static string FormatErrorMessage(string error, string contentType) } else if (ContentHelper.IsJson(contentType)) { - JsonNode jsonNode = JsonNode.Parse(error); + JsonNode jsonNode = JsonNode.Parse(error); JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; string jsonString = jsonNode.ToJsonString(options); From ce5ca665698774f9bbff4fa28d80e0ab8168cfd1 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 01:18:00 +0100 Subject: [PATCH 19/30] Add back NewLines --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 77f0f0d3f0a..f4e0cb8704d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1959,7 +1959,7 @@ private static string FormatErrorMessage(string error, string contentType) if (xmlString.Length > 0) { converted = true; - formattedError = xmlString; + formattedError = Environment.NewLine + xmlString; } } else if (ContentHelper.IsJson(contentType)) @@ -1971,7 +1971,7 @@ private static string FormatErrorMessage(string error, string contentType) if (jsonString.Length > 0) { converted = true; - formattedError = jsonString; + formattedError = Environment.NewLine + jsonString; } } } From d0f88d4234f8d99fd2e0ff930909bdb66b996e2b Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:58:23 +0100 Subject: [PATCH 20/30] add tests for xml and json errors --- .../WebCmdlets.Tests.ps1 | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 0a84f60cd76..a969524f375 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -745,6 +745,33 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } + It "Validate Invoke-WebRequest returns errors in exception" -TestCases @( + @{ type = "XML" + query = @{ + contenttype = 'application/xml' + body = '418I am a teapot!!!' + statuscode = 418 + } + expectederror = "`n`n 418`n I am a teapot!!!`n" + } + + @{ type = "Json" + query = @{ + contenttype = 'application/json' + body = '{"error":{"code":"418", "message":"I am a teapot!!!"}}' + statuscode = 418 + } + expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" + } + ) { + param($type, $query) + $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'" From 28d8152a33654986071caa7353235b99715e4167 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:58:54 +0100 Subject: [PATCH 21/30] spacing --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index a969524f375..b3b23222006 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -761,7 +761,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { body = '{"error":{"code":"418", "message":"I am a teapot!!!"}}' statuscode = 418 } - expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" + expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" } ) { param($type, $query) From f2992b3cc348c4b86522d08c5bf0ddafdc9cdfac Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 17:00:08 +0100 Subject: [PATCH 22/30] spaces --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index b3b23222006..2cb267e9bdb 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -761,7 +761,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { body = '{"error":{"code":"418", "message":"I am a teapot!!!"}}' statuscode = 418 } - expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" + expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" } ) { param($type, $query) From 7618c2b2fc5b2da49ba4e388b80e2d4295b6e576 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 17:25:53 +0100 Subject: [PATCH 23/30] fix? --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 2cb267e9bdb..ab5e6132641 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -751,8 +751,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { contenttype = 'application/xml' body = '418I am a teapot!!!' statuscode = 418 + responsephrase = "I am a teapot" } - expectederror = "`n`n 418`n I am a teapot!!!`n" + expectederror = "`n`n 418`n I am a teapot!!!`n" } @{ type = "Json" @@ -760,6 +761,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { contenttype = 'application/json' body = '{"error":{"code":"418", "message":"I am a teapot!!!"}}' statuscode = 418 + responsephrase = "I am a teapot" } expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" } From 7edeedb3870758ac0b87d08b142b52c1e6dbfdfd Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 17:27:53 +0100 Subject: [PATCH 24/30] fix param --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ab5e6132641..2a80051b880 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -766,7 +766,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" } ) { - param($type, $query) + param($query, $expectederror) $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command From a5ccbfe20eb805571c5a5d92be71131db6b9efc2 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 30 Jan 2023 18:33:49 +0100 Subject: [PATCH 25/30] different newline if windows --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 2a80051b880..b86e9357f87 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -753,7 +753,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { statuscode = 418 responsephrase = "I am a teapot" } - expectederror = "`n`n 418`n I am a teapot!!!`n" + expectederror = $IsWindows ? "`r`n`r`n 418`r`n I am a teapot!!!`r`n" : "`n`n 418`n I am a teapot!!!`n" } @{ type = "Json" @@ -763,7 +763,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { statuscode = 418 responsephrase = "I am a teapot" } - expectederror = "`n{`n `"error`": {`n `"code`": `"418`",`n `"message`": `"I am a teapot!!!`"`n }`n}" + 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) From cc5a5c54e7617e401c4f96ad490c985a2724ea2d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 8 Feb 2023 15:42:11 +0100 Subject: [PATCH 26/30] small changes Xml error --- .../Common/WebRequestPSCmdlet.Common.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 99b19c8271b..39e07612fb5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1831,22 +1831,24 @@ private static string FormatErrorMessage(string error, string contentType) { if (ContentHelper.IsXml(contentType)) { - StringBuilder stringBuilder = new StringBuilder(); XmlDocument doc = new XmlDocument(); doc.LoadXml(error); - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Indent = true; - settings.NewLineOnAttributes = true; - settings.OmitXmlDeclaration = true; - settings.Encoding = ContentHelper.GetDefaultEncoding(); + XmlWriterSettings settings = new XmlWriterSettings { + Indent = true, + NewLineOnAttributes = true, + OmitXmlDeclaration = true, + Encoding = ContentHelper.GetDefaultEncoding() + }; + if (doc.FirstChild is XmlDeclaration) { XmlDeclaration decl = doc.FirstChild as XmlDeclaration; settings.Encoding = Encoding.GetEncoding(decl.Encoding); } - XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); + StringBuilder stringBuilder = new StringBuilder(); + using XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); doc.Save(xmlWriter); string xmlString = stringBuilder.ToString(); From 7b04f41d373c75cc7d8ae223d8dad3020b0911bc Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 8 Feb 2023 15:45:06 +0100 Subject: [PATCH 27/30] ewmoved encoding the default is already UTF8 --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 39e07612fb5..a1d29392be9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1837,8 +1837,7 @@ private static string FormatErrorMessage(string error, string contentType) XmlWriterSettings settings = new XmlWriterSettings { Indent = true, NewLineOnAttributes = true, - OmitXmlDeclaration = true, - Encoding = ContentHelper.GetDefaultEncoding() + OmitXmlDeclaration = true }; if (doc.FirstChild is XmlDeclaration) From c7ff97cfa98b14a335476ba6f71095ff76dc085c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 8 Feb 2023 19:00:05 +0100 Subject: [PATCH 28/30] follow suggestions --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index a1d29392be9..1aa9c1a039d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1831,7 +1831,7 @@ private static string FormatErrorMessage(string error, string contentType) { if (ContentHelper.IsXml(contentType)) { - XmlDocument doc = new XmlDocument(); + XmlDocument doc = new(); doc.LoadXml(error); XmlWriterSettings settings = new XmlWriterSettings { @@ -1846,7 +1846,7 @@ private static string FormatErrorMessage(string error, string contentType) settings.Encoding = Encoding.GetEncoding(decl.Encoding); } - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder stringBuilder = new(); using XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings); doc.Save(xmlWriter); string xmlString = stringBuilder.ToString(); From 586583a03ea1e10989a3f0fb5e90ffce3f8bca60 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 8 Feb 2023 19:08:35 +0100 Subject: [PATCH 29/30] remove bool converted --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 1aa9c1a039d..0ee8b9b44e0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1824,7 +1824,6 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo private static string FormatErrorMessage(string error, string contentType) { - bool converted = false; string formattedError = null; try @@ -1853,7 +1852,6 @@ private static string FormatErrorMessage(string error, string contentType) if (xmlString.Length > 0) { - converted = true; formattedError = Environment.NewLine + xmlString; } } @@ -1865,7 +1863,6 @@ private static string FormatErrorMessage(string error, string contentType) if (jsonString.Length > 0) { - converted = true; formattedError = Environment.NewLine + jsonString; } } @@ -1875,7 +1872,7 @@ private static string FormatErrorMessage(string error, string contentType) // Ignore errors } - if (converted is false) + if (formattedError is null) { // Remove HTML tags making it easier to read formattedError = System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty); From f2a21135157f087b7c3812d08dc92f661c69c591 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 8 Feb 2023 19:12:26 +0100 Subject: [PATCH 30/30] remove String.Length > 0 --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 0ee8b9b44e0..c231aeac5ab 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1850,10 +1850,7 @@ private static string FormatErrorMessage(string error, string contentType) doc.Save(xmlWriter); string xmlString = stringBuilder.ToString(); - if (xmlString.Length > 0) - { - formattedError = Environment.NewLine + xmlString; - } + formattedError = Environment.NewLine + xmlString; } else if (ContentHelper.IsJson(contentType)) { @@ -1861,10 +1858,7 @@ private static string FormatErrorMessage(string error, string contentType) JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; string jsonString = jsonNode.ToJsonString(options); - if (jsonString.Length > 0) - { - formattedError = Environment.NewLine + jsonString; - } + formattedError = Environment.NewLine + jsonString; } } catch @@ -1872,7 +1866,7 @@ private static string FormatErrorMessage(string error, string contentType) // Ignore errors } - if (formattedError is null) + if (string.IsNullOrEmpty(formattedError)) { // Remove HTML tags making it easier to read formattedError = System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty);