diff --git a/handler.go b/handler.go index 41abea2..4136c19 100644 --- a/handler.go +++ b/handler.go @@ -23,7 +23,12 @@ func NewAWSLambdaHandler(ctx context.Context, opts *slog.HandlerOptions) slog.Ha } // Retrieve AWS Request ID and lambda function arn - lc, _ := lambdacontext.FromContext(ctx) + lc, found := lambdacontext.FromContext(ctx) + + if !found { + return slog.NewJSONHandler(os.Stdout, opts) + } + requestID := lc.AwsRequestID arn := lc.InvokedFunctionArn diff --git a/handler_test.go b/handler_test.go index 3179be8..f99e8af 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1,6 +1,7 @@ package slogawslambda import ( + "context" "log/slog" "os" "testing" @@ -49,3 +50,11 @@ func TestGetLogLevel(t *testing.T) { }) } } + +func TestUsingNonAwsContextShouldNotError(t *testing.T) { + ctx := context.Background() + + h := NewAWSLambdaHandler(ctx, nil) + + assert.NotNil(t, h) +}