From 71f09fafab7c3e7402b85727a5a32ab7c9416f34 Mon Sep 17 00:00:00 2001 From: Scott Beddall <45376673+scbedd@users.noreply.github.com> Date: Tue, 28 May 2024 11:40:33 -0700 Subject: [PATCH] Minimize exceptions thrown from `BodyKeySanitizer` (#8313) * dont attempt to parse bodys that aren't marked as json. avoid throwing so many exceptions * cleanup usings --- .../Sanitizers/BodyKeySanitizer.cs | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs index ac638be0c13..7ed5909994d 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs @@ -2,7 +2,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; -using System.Net; namespace Azure.Sdk.Tools.TestProxy.Sanitizers { @@ -44,25 +43,28 @@ public BodyKeySanitizer(string jsonPath, string value = "Sanitized", string rege public override string SanitizeTextBody(string contentType, string body) { bool sanitized = false; - JToken jsonO; + JToken jsonO = null; - try + if (contentType.ToLower().Contains("json")) { - // Prevent default behavior where JSON.NET will convert DateTimeOffset - // into a DateTime. - if (!LegacyConvertJsonDateTokens) + try { - jsonO = JsonConvert.DeserializeObject(body, SerializerSettings); + // Prevent default behavior where JSON.NET will convert DateTimeOffset + // into a DateTime. + if (!LegacyConvertJsonDateTokens) + { + jsonO = JsonConvert.DeserializeObject(body, SerializerSettings); + } + else + { + jsonO = JToken.Parse(body); + } } - else + catch (JsonReaderException) { - jsonO = JToken.Parse(body); + return body; } } - catch(JsonReaderException) - { - return body; - } if (jsonO != null) {