Skip to content

Commit 71f09fa

Browse files
authored
Minimize exceptions thrown from BodyKeySanitizer (#8313)
* dont attempt to parse bodys that aren't marked as json. avoid throwing so many exceptions * cleanup usings
1 parent 4e38840 commit 71f09fa

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Linq;
44
using System;
5-
using System.Net;
65

76
namespace Azure.Sdk.Tools.TestProxy.Sanitizers
87
{
@@ -44,25 +43,28 @@ public BodyKeySanitizer(string jsonPath, string value = "Sanitized", string rege
4443
public override string SanitizeTextBody(string contentType, string body)
4544
{
4645
bool sanitized = false;
47-
JToken jsonO;
46+
JToken jsonO = null;
4847

49-
try
48+
if (contentType.ToLower().Contains("json"))
5049
{
51-
// Prevent default behavior where JSON.NET will convert DateTimeOffset
52-
// into a DateTime.
53-
if (!LegacyConvertJsonDateTokens)
50+
try
5451
{
55-
jsonO = JsonConvert.DeserializeObject<JToken>(body, SerializerSettings);
52+
// Prevent default behavior where JSON.NET will convert DateTimeOffset
53+
// into a DateTime.
54+
if (!LegacyConvertJsonDateTokens)
55+
{
56+
jsonO = JsonConvert.DeserializeObject<JToken>(body, SerializerSettings);
57+
}
58+
else
59+
{
60+
jsonO = JToken.Parse(body);
61+
}
5662
}
57-
else
63+
catch (JsonReaderException)
5864
{
59-
jsonO = JToken.Parse(body);
65+
return body;
6066
}
6167
}
62-
catch(JsonReaderException)
63-
{
64-
return body;
65-
}
6668

6769
if (jsonO != null)
6870
{

0 commit comments

Comments
 (0)