Skip to content

Commit

Permalink
Minimize exceptions thrown from BodyKeySanitizer (#8313)
Browse files Browse the repository at this point in the history
* dont attempt to parse bodys that aren't marked as json. avoid throwing so many exceptions

* cleanup usings
  • Loading branch information
scbedd authored May 28, 2024
1 parent 4e38840 commit 71f09fa
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net;

namespace Azure.Sdk.Tools.TestProxy.Sanitizers
{
Expand Down Expand Up @@ -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<JToken>(body, SerializerSettings);
// Prevent default behavior where JSON.NET will convert DateTimeOffset
// into a DateTime.
if (!LegacyConvertJsonDateTokens)
{
jsonO = JsonConvert.DeserializeObject<JToken>(body, SerializerSettings);
}
else
{
jsonO = JToken.Parse(body);
}
}
else
catch (JsonReaderException)
{
jsonO = JToken.Parse(body);
return body;
}
}
catch(JsonReaderException)
{
return body;
}

if (jsonO != null)
{
Expand Down

0 comments on commit 71f09fa

Please sign in to comment.