Skip to content

Commit 159120e

Browse files
committed
Fix Tracing when exception is thrown. Prevent Tracing from throwing InvalidOperationException
1 parent 263c170 commit 159120e

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,25 @@ public T OnException<T>(AspectEventArgs eventArgs, Exception exception)
163163
if (CaptureError())
164164
{
165165
var nameSpace = GetNamespace();
166-
166+
167+
var message =
168+
"Exception type " + exception.GetType() + Environment.NewLine +
169+
"Exception message: " + exception.Message + Environment.NewLine +
170+
"Stack trace: " + exception.StackTrace + Environment.NewLine;
171+
if (exception.InnerException != null)
172+
{
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";
178+
}
179+
167180
_xRayRecorder.AddMetadata
168181
(
169182
nameSpace,
170183
$"{eventArgs.Name} error",
171-
exception
184+
message
172185
);
173186
}
174187

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ public void OnException_WhenTracerCaptureErrorEnvironmentVariableIsTrue_Captures
590590
configurations.Setup(c => c.TracerCaptureError).Returns(true);
591591
var recorder = new Mock<IXRayRecorder>();
592592
var exception = new Exception("Test Exception");
593-
593+
var message = GetException(exception);
594+
594595
var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.EnvironmentVariable,
595596
configurations.Object, recorder.Object);
596597
var eventArgs = new AspectEventArgs {Name = methodName};
@@ -604,7 +605,7 @@ public void OnException_WhenTracerCaptureErrorEnvironmentVariableIsTrue_Captures
604605
v.AddMetadata(
605606
It.Is<string>(i => i == nameSpace),
606607
It.Is<string>(i => i == $"{methodName} error"),
607-
It.Is<Exception>(i => i == exception
608+
It.Is<string>(i => i == message
608609
)
609610
), Times.Once);
610611
}
@@ -650,7 +651,8 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
650651
configurations.Setup(c => c.TracingDisabled).Returns(false);
651652
var recorder = new Mock<IXRayRecorder>();
652653
var exception = new Exception("Test Exception");
653-
654+
var message = GetException(exception);
655+
654656
var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.Error,
655657
configurations.Object, recorder.Object);
656658
var eventArgs = new AspectEventArgs {Name = methodName};
@@ -664,7 +666,7 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
664666
v.AddMetadata(
665667
It.Is<string>(i => i == nameSpace),
666668
It.Is<string>(i => i == $"{methodName} error"),
667-
It.Is<Exception>(i => i == exception
669+
It.Is<string>(i => i == message
668670
)
669671
), Times.Once);
670672
}
@@ -680,7 +682,8 @@ public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()
680682
configurations.Setup(c => c.TracingDisabled).Returns(false);
681683
var recorder = new Mock<IXRayRecorder>();
682684
var exception = new Exception("Test Exception");
683-
685+
var message = GetException(exception);
686+
684687
var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.ResponseAndError,
685688
configurations.Object, recorder.Object);
686689
var eventArgs = new AspectEventArgs {Name = methodName};
@@ -694,7 +697,7 @@ public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()
694697
v.AddMetadata(
695698
It.Is<string>(i => i == nameSpace),
696699
It.Is<string>(i => i == $"{methodName} error"),
697-
It.Is<Exception>(i => i == exception
700+
It.Is<string>(i => i == message
698701
)
699702
), Times.Once);
700703
}
@@ -761,6 +764,27 @@ public void OnException_WhenTracerCaptureModeIsDisabled_DoesNotCaptureError()
761764

762765
#endregion
763766

767+
#region Utilities
768+
769+
static string GetException(Exception exception)
770+
{
771+
var message =
772+
"Exception type " + exception.GetType() + Environment.NewLine +
773+
"Exception message: " + exception.Message + Environment.NewLine +
774+
"Stack trace: " + exception.StackTrace + Environment.NewLine;
775+
if (exception.InnerException != null)
776+
{
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";
782+
}
783+
return message;
784+
}
785+
786+
#endregion
787+
764788
#region OnExit Tests
765789

766790
[Fact]

0 commit comments

Comments
 (0)