Skip to content

Commit 0af4fc8

Browse files
authored
Change try/catch around JSON serialization to a general Exception catch (#106)
* Change try/catch around JSON serialization to a general Exception catch * Update package version * Change a log message from e.Message() to e.ToString()
1 parent bc50721 commit 0af4fc8

File tree

1 file changed

+8
-3
lines changed
  • IntelliTect.TestTools.TestFramework/IntelliTect.TestTools.TestFramework

1 file changed

+8
-3
lines changed

IntelliTect.TestTools.TestFramework/IntelliTect.TestTools.TestFramework/TestBuilder.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,19 @@ public void ExecuteTestCase()
208208

209209
private static string GetObjectDataAsJsonString(object obj)
210210
{
211-
211+
// JsonSerializer.Serialize has some different throw behavior between versions.
212+
// One version threw an exception that occurred on a property, which happened to be a Selenium WebDriverException.
213+
// In this one specific case, catch all exceptions and move on to provide standard behavior to all package consumers.
214+
// TL;DR: we don't want logging failures to interrupt the test run.
212215
try
213216
{
214217
return JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true });
215218
}
216-
catch (JsonException e)
219+
#pragma warning disable CA1031 // Do not catch general exception types
220+
catch (Exception e)
221+
#pragma warning restore CA1031 // Do not catch general exception types
217222
{
218-
return $"Unable to serialize object {obj?.GetType()} to JSON. Mark the relevant property with the [JsonIgnore] attribute: {e.Message}";
223+
return $"Unable to serialize object {obj?.GetType()} to JSON. Mark the relevant property with the [JsonIgnore] attribute: {e}";
219224
}
220225

221226
}

0 commit comments

Comments
 (0)