Skip to content

Commit 073da9c

Browse files
committed
test stacktrace with decorated method
1 parent 1ae3c0c commit 073da9c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/ExceptionFunctionHandler.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ public Task<string> HandleDecoratorOutsideHandler(string input)
2222

2323
return Task.FromResult(input.ToUpper(CultureInfo.InvariantCulture));
2424
}
25+
26+
[Metrics(Namespace = "ns", Service = "svc")]
27+
public Task<string> HandleDecoratorOutsideHandlerException(string input)
28+
{
29+
MethodDecorated();
30+
31+
Metrics.AddMetric($"Metric Name", 1, MetricUnit.Count);
32+
33+
ThisThrowsDecorated();
34+
35+
return Task.FromResult(input.ToUpper(CultureInfo.InvariantCulture));
36+
}
2537

2638
[Metrics(Namespace = "ns", Service = "svc")]
2739
private void MethodDecorated()
2840
{
29-
// NOOP
3041
Metrics.AddMetric($"Metric Name", 1, MetricUnit.Count);
3142
Metrics.AddMetric($"Metric Name Decorated", 1, MetricUnit.Count);
3243
}
@@ -35,4 +46,10 @@ private void ThisThrows()
3546
{
3647
throw new NullReferenceException();
3748
}
49+
50+
[Metrics(Namespace = "ns", Service = "svc")]
51+
private void ThisThrowsDecorated()
52+
{
53+
throw new NullReferenceException();
54+
}
3855
}

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/ExceptionFunctionHandlerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ public async Task Stack_Trace_Included_When_Decorator_Present()
2020
// Assert
2121
var tracedException = await Assert.ThrowsAsync<NullReferenceException>(Handle);
2222
Assert.StartsWith("at AWS.Lambda.Powertools.Metrics.Tests.Handlers.ExceptionFunctionHandler.ThisThrows()", tracedException.StackTrace?.TrimStart());
23+
}
24+
25+
[Fact]
26+
public async Task Stack_Trace_Included_When_Decorator_Present_In_Method()
27+
{
28+
// Arrange
29+
Metrics.ResetForTest();
30+
var handler = new ExceptionFunctionHandler();
2331

32+
// Act
33+
Task Handle() => handler.HandleDecoratorOutsideHandlerException("whatever");
34+
35+
// Assert
36+
var tracedException = await Assert.ThrowsAsync<NullReferenceException>(Handle);
37+
Assert.StartsWith("at AWS.Lambda.Powertools.Metrics.Tests.Handlers.ExceptionFunctionHandler.__a$_around_ThisThrows", tracedException.StackTrace?.TrimStart());
2438
}
2539

2640
[Fact]

0 commit comments

Comments
 (0)