Skip to content

Bug: LoggingLambdaContext.Extract thorws a NullReferenceException if ILambdaContext is null and not the first parameter in a method #792

@gregsinclair42

Description

@gregsinclair42

Expected Behaviour

LoggingLambdaContext.Extract should check if the LambdaContext is not null regardless of if its position in the parameters list, and return false (the same way it does if other checks fail).

Current Behaviour

Currently, LoggingLambdaContext.Extract will throw an exception if the ILambdaContext is null and not the first parameter in a lambda method.

Code snippet

// add this unit test to https://github.com/aws-powertools/powertools-lambda-dotnet/blob/develop/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Context/LambdaContextTest.cs and you will see the exception:

[Fact]
public void Extract_Extract_When_LambdaContext_Is_Null_But_Not_First_Parameter_Returns_False()
{
     // Arrange
     ILambdaContext lambdaContext = null;
     var args = Substitute.For<AspectEventArgs>();
     var method = Substitute.For<MethodInfo>();
     var parameter1 = Substitute.For<ParameterInfo>();
     var parameter2 = Substitute.For<ParameterInfo>();
     
     // Setup parameters
     parameter1.ParameterType.Returns(typeof(string));
     parameter2.ParameterType.Returns(typeof(ILambdaContext));
  
     // Setup method
     method.GetParameters().Returns(new[] { parameter1, parameter2 });
  
     // Setup args
     args.Method = method;
     args.Args = new object[] { "requestContext", lambdaContext }; 
     
     // Act && Assert
     LoggingLambdaContext.Clear();
     Assert.Null(LoggingLambdaContext.Instance);
     Assert.False(LoggingLambdaContext.Extract(args)); // exception will be thrown here, instead of returning false
}

Possible Solution

#791

Steps to Reproduce

As shown in the code above.

Powertools for AWS Lambda (.NET) version

latest

AWS Lambda function runtime

dotnet8

Debugging logs

Metadata

Metadata

Labels

area/loggingCore logging utilitybugUnexpected, reproducible and unintended software behaviour

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions