Skip to content

Commit 2e23dec

Browse files
committed
Use StringBuilder to create error message.
1 parent 159120e commit 2e23dec

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

libraries/src/AWS.Lambda.Powertools.Tracing/Internal/TracingAspectHandler.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Text;
1718
using AWS.Lambda.Powertools.Common;
1819

1920
namespace AWS.Lambda.Powertools.Tracing.Internal;
@@ -164,24 +165,25 @@ public T OnException<T>(AspectEventArgs eventArgs, Exception exception)
164165
{
165166
var nameSpace = GetNamespace();
166167

167-
var message =
168-
"Exception type " + exception.GetType() + Environment.NewLine +
169-
"Exception message: " + exception.Message + Environment.NewLine +
170-
"Stack trace: " + exception.StackTrace + Environment.NewLine;
168+
var sb = new StringBuilder();
169+
sb.AppendLine($"Exception type: {exception.GetType()}");
170+
sb.AppendLine($"Exception message: {exception.Message}");
171+
sb.AppendLine($"Stack trace: {exception.StackTrace}");
172+
171173
if (exception.InnerException != null)
172174
{
173-
message += "---BEGIN InnerException--- " + Environment.NewLine +
174-
"Exception type " + exception.InnerException.GetType() + Environment.NewLine +
175-
"Exception message: " + exception.InnerException.Message + Environment.NewLine +
176-
"Stack trace: " + exception.InnerException.StackTrace + Environment.NewLine +
177-
"---END Inner Exception";
175+
sb.AppendLine("---BEGIN InnerException--- ");
176+
sb.AppendLine($"Exception type {exception.InnerException.GetType()}");
177+
sb.AppendLine($"Exception message: {exception.InnerException.Message}");
178+
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
179+
sb.AppendLine("---END Inner Exception");
178180
}
179181

180182
_xRayRecorder.AddMetadata
181183
(
182184
nameSpace,
183185
$"{eventArgs.Name} error",
184-
message
186+
sb.ToString()
185187
);
186188
}
187189

libraries/tests/AWS.Lambda.Powertools.Tracing.Tests/TracingAttributeTest.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Linq;
18+
using System.Text;
1819
using AWS.Lambda.Powertools.Common;
1920
using AWS.Lambda.Powertools.Tracing.Internal;
2021
using Moq;
@@ -768,19 +769,20 @@ public void OnException_WhenTracerCaptureModeIsDisabled_DoesNotCaptureError()
768769

769770
static string GetException(Exception exception)
770771
{
771-
var message =
772-
"Exception type " + exception.GetType() + Environment.NewLine +
773-
"Exception message: " + exception.Message + Environment.NewLine +
774-
"Stack trace: " + exception.StackTrace + Environment.NewLine;
772+
var sb = new StringBuilder();
773+
sb.AppendLine($"Exception type: {exception.GetType()}");
774+
sb.AppendLine($"Exception message: {exception.Message}");
775+
sb.AppendLine($"Stack trace: {exception.StackTrace}");
776+
775777
if (exception.InnerException != null)
776778
{
777-
message += "---BEGIN InnerException--- " + Environment.NewLine +
778-
"Exception type " + exception.InnerException.GetType() + Environment.NewLine +
779-
"Exception message: " + exception.InnerException.Message + Environment.NewLine +
780-
"Stack trace: " + exception.InnerException.StackTrace + Environment.NewLine +
781-
"---END Inner Exception";
779+
sb.AppendLine("---BEGIN InnerException--- ");
780+
sb.AppendLine($"Exception type {exception.InnerException.GetType()}");
781+
sb.AppendLine($"Exception message: {exception.InnerException.Message}");
782+
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
783+
sb.AppendLine("---END Inner Exception");
782784
}
783-
return message;
785+
return sb.ToString();
784786
}
785787

786788
#endregion

0 commit comments

Comments
 (0)