-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathinvocationerror.go
More file actions
68 lines (53 loc) · 2.15 KB
/
invocationerror.go
File metadata and controls
68 lines (53 loc) · 2.15 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 (
"bytes"
"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 RuntimeErrorHandler interface {
RuntimeError(ctx context.Context, runtimeErrReq invoke.RuntimeErrorRequest) model.AppError
}
type invocationErrorHandler struct {
runtimeErrHandler RuntimeErrorHandler
}
func (h *invocationErrorHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
invokeID := chi.URLParam(request, "awsrequestid")
ctx := logging.WithInvokeID(request.Context(), invokeID)
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(request.Body); err != nil {
logging.Warn(ctx, "Failed to parse error body", "err", err)
rendering.RenderRequestEntityTooLarge(writer, request)
return
}
resp := invoke.NewRuntimeError(ctx, request, invokeID, buf.String())
err := h.runtimeErrHandler.RuntimeError(ctx, &resp)
logging.Warn(ctx, "Received Runtime error", "err", err)
if err == nil {
rendering.RenderAccepted(writer, request)
return
}
logging.Warn(ctx, "Runtime response error", "err", err)
switch err.ErrorType() {
case model.ErrorRuntimeInvalidInvokeId, model.ErrorRuntimeInvokeErrorInProgress:
rendering.RenderInvalidRequestID(writer, request)
case model.ErrorRuntimeInvokeTimeout:
rendering.RenderInvokeTimeout(writer, request)
case model.ErrorRuntimeInvokeResponseWasSent:
rendering.RenderInvalidRequestID(writer, request)
default:
logging.Error(ctx, "Received unexpected runtime error", "err", err)
rendering.RenderInternalServerError(writer, request)
}
}
func NewInvocationErrorHandler(runtimeErrHandler RuntimeErrorHandler) http.Handler {
return &invocationErrorHandler{
runtimeErrHandler: runtimeErrHandler,
}
}