-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathinvocationresponse.go
More file actions
68 lines (54 loc) · 2.33 KB
/
invocationresponse.go
File metadata and controls
68 lines (54 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package handler
import (
"context"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/invoke"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/logging"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/rapi/rendering"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/rapid/model"
)
type RuntimeResponseHandler interface {
RuntimeResponse(ctx context.Context, runtimeRespReq invoke.RuntimeResponseRequest) model.AppError
}
type invocationResponseHandler struct {
runtimeRespHandler RuntimeResponseHandler
}
func (h *invocationResponseHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
invokeID := chi.URLParam(request, "awsrequestid")
ctx := logging.WithInvokeID(request.Context(), invokeID)
logging.Debug(ctx, "Received Runtime Response")
resp := invoke.NewRuntimeResponse(ctx, request, invokeID)
err := h.runtimeRespHandler.RuntimeResponse(ctx, &resp)
if err == nil {
rendering.RenderAccepted(writer, request)
return
}
logging.Warn(ctx, "Runtime Response Error", "err", err)
switch err.ErrorType() {
case model.ErrorRuntimeInvalidInvokeId, model.ErrorRuntimeInvokeResponseInProgress:
rendering.RenderInvalidRequestID(writer, request)
case model.ErrorRuntimeInvokeTimeout, model.ErrorSandboxTimedout:
rendering.RenderInvokeTimeout(writer, request)
case model.ErrorRuntimeInvalidResponseModeHeader:
rendering.RenderInvalidFunctionResponseMode(writer, request)
case model.ErrorFunctionOversizedResponse:
rendering.RenderRequestEntityTooLarge(writer, request)
case model.ErrorRuntimeTruncatedResponse:
rendering.RenderTruncatedHTTPRequestError(writer, request)
default:
if trailerError := resp.TrailerError(); trailerError.ErrorType() != "" {
rendering.RenderAccepted(writer, request)
return
}
logging.Error(ctx, "unexpected error in runtime response", "err", err)
rendering.RenderInternalServerError(writer, request)
}
}
func NewInvocationResponseHandler(runtimeRespHandler RuntimeResponseHandler) http.Handler {
return &invocationResponseHandler{
runtimeRespHandler: runtimeRespHandler,
}
}