diff --git a/internal/wrapper/wrap_handler.go b/internal/wrapper/wrap_handler.go index 8cd994cd..a8e101f8 100644 --- a/internal/wrapper/wrap_handler.go +++ b/internal/wrapper/wrap_handler.go @@ -13,7 +13,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/DataDog/datadog-lambda-go/internal/extension" "github.com/DataDog/datadog-lambda-go/internal/logger" "github.com/aws/aws-lambda-go/lambda" @@ -133,10 +132,17 @@ func validateHandler(handler interface{}) error { return nil } -func callHandler(ctx context.Context, msg json.RawMessage, handler interface{}) (interface{}, error) { - ev, err := unmarshalEventForHandler(msg, handler) - if err != nil { - return nil, err +func callHandler(ctx context.Context, msg json.RawMessage, handler interface{}) (response interface{}, errResponse error) { + defer func() { + if r := recover(); r != nil { + response = nil + errResponse = fmt.Errorf("Panic: %v", r) + } + }() + + ev, errResponse := unmarshalEventForHandler(msg, handler) + if errResponse != nil { + return nil, errResponse } handlerType := reflect.TypeOf(handler) @@ -160,9 +166,6 @@ func callHandler(ctx context.Context, msg json.RawMessage, handler interface{}) handlerValue := reflect.ValueOf(handler) output := handlerValue.Call(args) - var response interface{} - var errResponse error - if len(output) > 0 { // If there are any output values, the last should always be an error val := output[len(output)-1].Interface() diff --git a/internal/wrapper/wrap_handler_test.go b/internal/wrapper/wrap_handler_test.go index 8c5fe3f0..0ce8d45b 100644 --- a/internal/wrapper/wrap_handler_test.go +++ b/internal/wrapper/wrap_handler_test.go @@ -270,6 +270,22 @@ func TestWrapHandlerReturnsErrorOnly(t *testing.T) { assert.Equal(t, nil, response) } +func TestWrapHandlerReturnsErrorWhenPanic(t *testing.T) { + called := false + panicRecoverErr := errors.New("Panic: some panic") + + handler := func(request mockNonProxyEvent) error { + called = true + panic("some panic") + } + + _, response, err := runHandlerWithJSON(t, "../testdata/non-proxy-no-headers.json", handler) + + assert.True(t, called) + assert.Equal(t, panicRecoverErr, err) + assert.Equal(t, nil, response) +} + func TestWrapHandlerReturnsOriginalHandlerIfInvalid(t *testing.T) { var handler interface{} = func(arg1, arg2, arg3 int) (int, error) { diff --git a/tests/integration_tests/hello/go.mod b/tests/integration_tests/hello/go.mod index 1a775d7b..d05c9946 100644 --- a/tests/integration_tests/hello/go.mod +++ b/tests/integration_tests/hello/go.mod @@ -7,11 +7,11 @@ toolchain go1.21.5 require ( github.com/DataDog/datadog-lambda-go v1.14.0 github.com/aws/aws-lambda-go v1.46.0 - gopkg.in/DataDog/dd-trace-go.v1 v1.60.0 + gopkg.in/DataDog/dd-trace-go.v1 v1.60.3 ) require ( - github.com/DataDog/appsec-internal-go v1.4.0 // indirect + github.com/DataDog/appsec-internal-go v1.4.1 // indirect github.com/DataDog/datadog-agent/pkg/obfuscate v0.50.2 // indirect github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.50.2 // indirect github.com/DataDog/datadog-go/v5 v5.5.0 // indirect @@ -67,7 +67,7 @@ require ( golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect google.golang.org/grpc v1.61.0 // indirect - google.golang.org/protobuf v1.32.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect ) diff --git a/tests/integration_tests/hello/go.sum b/tests/integration_tests/hello/go.sum index 5c7c12c3..31e870ca 100644 --- a/tests/integration_tests/hello/go.sum +++ b/tests/integration_tests/hello/go.sum @@ -3,6 +3,7 @@ github.com/DataDog/appsec-internal-go v1.0.2 h1:Z+YWPlkQN+324zIk+BzKlPA1/6guKgGm github.com/DataDog/appsec-internal-go v1.0.2/go.mod h1:+Y+4klVWKPOnZx6XESG7QHydOaUGEXyH2j/vSg9JiNM= github.com/DataDog/appsec-internal-go v1.4.0 h1:KFI8ElxkJOgpw+cUm9TXK/jh5EZvRaWM07sXlxGg9Ck= github.com/DataDog/appsec-internal-go v1.4.0/go.mod h1:ONW8aV6R7Thgb4g0bB9ZQCm+oRgyz5eWiW7XoQ19wIc= +github.com/DataDog/appsec-internal-go v1.4.1/go.mod h1:rmZ+tpq5ZPKmeOUMYjWFg+q1mRd13mxZwSLBG+xa1ik= github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 h1:bUMSNsw1iofWiju9yc1f+kBd33E3hMJtq9GuU602Iy8= github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0/go.mod h1:HzySONXnAgSmIQfL6gOv9hWprKJkx8CicuXuUbmgWfo= github.com/DataDog/datadog-agent/pkg/obfuscate v0.50.2 h1:y08IzbpFM/HBaKfgayFZe1FpcbZn6bVPXoZ++93vxv8= @@ -316,10 +317,12 @@ google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cn google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/DataDog/dd-trace-go.v1 v1.58.0 h1:ixIUarsu0RrOt7xfdrE5YSFvjgaWsP3cC3G342jTIuw= gopkg.in/DataDog/dd-trace-go.v1 v1.58.0/go.mod h1:SmnEjjV9ZQr4MWRSUYEpoPyNtmtRK5J6UuJdAma+Yxw= gopkg.in/DataDog/dd-trace-go.v1 v1.60.0 h1:hjDiU6PWRgMoUeSJkXdtimUP76cFzREPIGIIQJD0mYU= gopkg.in/DataDog/dd-trace-go.v1 v1.60.0/go.mod h1:6aArYrAHjnuaofJ3lKuSRQbhrBx1LcSpiEYCIScJE5Y= +gopkg.in/DataDog/dd-trace-go.v1 v1.60.3/go.mod h1:XF/Y0lFGnmgedNXnltCm6hXkt9iwyeVVVFbKhJvVwtY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=