Skip to content

Commit dcc3705

Browse files
committed
add test for inner exception
1 parent e9da1aa commit dcc3705

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public string[] HandleWithCaptureModeError(bool exception = false)
6767
throw new Exception("Failed");
6868
return new[] { "A", "B" };
6969
}
70+
71+
[Tracing(CaptureMode = TracingCaptureMode.Error)]
72+
public string[] HandleWithCaptureModeErrorInner(bool exception = false)
73+
{
74+
if (exception)
75+
throw new Exception("Failed", new Exception("Inner Exception!!"));
76+
return new[] { "A", "B" };
77+
}
7078

7179
[Tracing(CaptureMode = TracingCaptureMode.Disabled)]
7280
public string[] HandleWithCaptureModeDisabled(bool exception = false)

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,34 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
514514
var handlerErrorMessage = metadata.Values.Cast<string>().First();
515515
Assert.Contains(handlerErrorMessage, GetException(exception));
516516
}
517+
518+
[Fact]
519+
public void OnException_WhenTracerCaptureModeIsError_CapturesError_Inner_Exception()
520+
{
521+
// Arrange
522+
Environment.SetEnvironmentVariable("LAMBDA_TASK_ROOT", "AWS");
523+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "POWERTOOLS");
524+
525+
// Act
526+
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
527+
528+
var exception = Record.Exception(() =>
529+
{
530+
_handler.HandleWithCaptureModeErrorInner(true);
531+
});
532+
var subSegment = segment.Subsegments[0];
533+
534+
// Assert
535+
Assert.NotNull(exception);
536+
Assert.True(segment.IsSubsegmentsAdded);
537+
Assert.Single(segment.Subsegments);
538+
Assert.True(subSegment.IsMetadataAdded);
539+
Assert.True(subSegment.Metadata.ContainsKey("POWERTOOLS"));
540+
var metadata = subSegment.Metadata["POWERTOOLS"];
541+
Assert.Equal("HandleWithCaptureModeErrorInner error", metadata.Keys.Cast<string>().First());
542+
Assert.NotNull(exception.InnerException);
543+
Assert.Equal("Inner Exception!!",exception.InnerException.Message);
544+
}
517545

518546
[Fact]
519547
public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()

0 commit comments

Comments
 (0)