Skip to content

Commit

Permalink
Merge pull request #4 from jbleduigou/3-using-non-aws-context-will-ca…
Browse files Browse the repository at this point in the history
…use-a-panic

fix: 🐛 verify that lambda context was found
  • Loading branch information
jbleduigou committed Apr 3, 2024
2 parents c6cfde7 + 991c4e9 commit 901fd50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slogawslambda

import (
"context"
"log/slog"
"os"
"testing"
Expand Down Expand Up @@ -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)
}

0 comments on commit 901fd50

Please sign in to comment.