handleInvoke returns an error if the function panics, or some other non-recoverable error occurred
(invoke *invoke, handler *handlerOptions)
| 41 | |
| 42 | // handleInvoke returns an error if the function panics, or some other non-recoverable error occurred |
| 43 | func handleInvoke(invoke *invoke, handler *handlerOptions) error { |
| 44 | // set the deadline |
| 45 | deadline, err := parseDeadline(invoke) |
| 46 | if err != nil { |
| 47 | return reportFailure(invoke, lambdaErrorResponse(err)) |
| 48 | } |
| 49 | ctx, cancel := context.WithDeadline(handler.baseContext, deadline) |
| 50 | defer cancel() |
| 51 | |
| 52 | // set the invoke metadata values |
| 53 | lc := lambdacontext.LambdaContext{ |
| 54 | AwsRequestID: invoke.id, |
| 55 | InvokedFunctionArn: invoke.headers.Get(headerInvokedFunctionARN), |
| 56 | TenantID: invoke.headers.Get(headerTenantID), |
| 57 | } |
| 58 | if err := parseClientContext(invoke, &lc.ClientContext); err != nil { |
| 59 | return reportFailure(invoke, lambdaErrorResponse(err)) |
| 60 | } |
| 61 | if err := parseCognitoIdentity(invoke, &lc.Identity); err != nil { |
| 62 | return reportFailure(invoke, lambdaErrorResponse(err)) |
| 63 | } |
| 64 | ctx = lambdacontext.NewContext(ctx, &lc) |
| 65 | |
| 66 | // set the trace id |
| 67 | traceID := invoke.headers.Get(headerTraceID) |
| 68 | if lambdacontext.MaxConcurrency() == 1 { |
| 69 | os.Setenv("_X_AMZN_TRACE_ID", traceID) |
| 70 | } |
| 71 | // nolint:staticcheck |
| 72 | ctx = context.WithValue(ctx, "x-amzn-trace-id", traceID) |
| 73 | |
| 74 | // call the handler, marshal any returned error |
| 75 | response, invokeErr := callBytesHandlerFunc(ctx, invoke.payload.Bytes(), handler.handlerFunc) |
| 76 | if invokeErr != nil { |
| 77 | if err := reportFailure(invoke, invokeErr); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | if invokeErr.ShouldExit { |
| 81 | return fmt.Errorf("calling the handler function resulted in a panic, the process should exit") |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | // if the response needs to be closed (ex: net.Conn, os.File), ensure it's closed before the next invoke to prevent a resource leak |
| 86 | if response, ok := response.(io.Closer); ok { |
| 87 | defer response.Close() |
| 88 | } |
| 89 | |
| 90 | // if the response defines a content-type, plumb it through |
| 91 | contentType := contentTypeBytes |
| 92 | type ContentType interface{ ContentType() string } |
| 93 | if response, ok := response.(ContentType); ok { |
| 94 | contentType = response.ContentType() |
| 95 | } |
| 96 | |
| 97 | if err := invoke.success(response, contentType); err != nil { |
| 98 | return fmt.Errorf("unexpected error occurred when sending the function functionResponse to the API: %v", err) |
| 99 | } |
| 100 |
no test coverage detected
searching dependent graphs…