From 460ca13c59a84ddea98b8c02177d41d3bb1b7f25 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Fri, 21 Jan 2022 14:27:49 +0100 Subject: [PATCH] Guard against unformattable caller name --- Src/FluentAssertions/Execution/MessageBuilder.cs | 9 ++++++++- docs/_pages/releases.md | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Src/FluentAssertions/Execution/MessageBuilder.cs b/Src/FluentAssertions/Execution/MessageBuilder.cs index c580c5bd34..d2c3a60471 100644 --- a/Src/FluentAssertions/Execution/MessageBuilder.cs +++ b/Src/FluentAssertions/Execution/MessageBuilder.cs @@ -91,7 +91,14 @@ private string FormatArgumentPlaceholders(string failureMessage, object[] failur { string[] values = failureArgs.Select(a => Formatter.ToString(a, formattingOptions)).ToArray(); - return string.Format(CultureInfo.InvariantCulture, failureMessage, values); + try + { + return string.Format(CultureInfo.InvariantCulture, failureMessage, values); + } + catch (FormatException formatException) + { + return $"**WARNING** failure message '{failureMessage}' could not be formatted with string.Format{Environment.NewLine}{formatException.StackTrace}"; + } } private string SanitizeReason(string reason) diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index cf5bcc54ac..d8cfccc21a 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -21,6 +21,7 @@ sidebar: * `OccurredEvent` ordering on monitored object is now done via thread-safe counter - [#1773](https://github.com/fluentassertions/fluentassertions/pull/1773) * Avoid a `NullReferenceException` when testing an application compiled with .NET Native - [#1776](https://github.com/fluentassertions/fluentassertions/pull/1776) * `[Not]Contain(key, value)` for dictionary-like enumerables incorrectly checked if the key was present - [#1786](https://github.com/fluentassertions/fluentassertions/pull/1786) +* Avoid throwing a `FormatException` when caller name determination returns an unformattable string - [#1788](https://github.com/fluentassertions/fluentassertions/pull/1788) ## 6.3.0