Skip to content

Commit 333b5d6

Browse files
committed
Fixed MassTransit#6168 - Trim exception messages and stack traces to avoid overloading the message transport with extensively long headers
1 parent f82caea commit 333b5d6

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/MassTransit.Abstractions/Util/ExceptionUtil.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ namespace MassTransit.Util
88

99
public static class ExceptionUtil
1010
{
11+
static readonly Regex _trim;
1112
static readonly Regex _cleanup;
1213

1314
static ExceptionUtil()
1415
{
1516
const string awaiter = @"at System\.Runtime\.CompilerServices\.TaskAwaiter.*";
1617
const string exception = @"at System\.Runtime\.ExceptionServices\.ExceptionDispatchInfo.*";
18+
const string dependency = @"at MassTransit\.DependencyInjection.*";
1719

18-
_cleanup = new Regex(@"\n\s+(" + string.Join("|", awaiter, exception) + ")+",
19-
RegexOptions.Multiline | RegexOptions.Compiled);
20+
_cleanup = new Regex(@"\n\s+(" + string.Join("|", awaiter, exception, dependency) + ")+", RegexOptions.Multiline | RegexOptions.Compiled);
21+
_trim = new Regex(@"in\s.*MassTransit.*\.cs.*$", RegexOptions.Multiline | RegexOptions.Compiled);
2022
}
2123

2224
public static string GetMessage(Exception exception)
2325
{
2426
try
2527
{
26-
return exception.Message ?? $"An exception of type {exception.GetType()} was thrown but the message was null.";
28+
var exceptionMessage = exception.Message ?? $"An exception of type {exception.GetType()} was thrown but the message was null.";
29+
30+
if (exceptionMessage.Length > 2048)
31+
exceptionMessage = exceptionMessage.Substring(0, 2048);
32+
33+
return exceptionMessage;
2734
}
2835
catch
2936
{
@@ -37,7 +44,13 @@ public static string GetStackTrace(Exception? exception)
3744
if (string.IsNullOrWhiteSpace(stackTrace))
3845
return "";
3946

40-
return _cleanup.Replace(stackTrace, "");
47+
stackTrace = _cleanup.Replace(stackTrace, "");
48+
stackTrace = _trim.Replace(stackTrace, "");
49+
50+
if (stackTrace.Length > 2048)
51+
stackTrace = stackTrace.Substring(0, 2048);
52+
53+
return stackTrace;
4154
}
4255

4356
[Obsolete("This method is obsolete and will be removed in a future version.")]

0 commit comments

Comments
 (0)