forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invocationerror.go
156 lines (123 loc) · 4.82 KB
/
invocationerror.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package handler
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/fatalerror"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/interop"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/model"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/appctx"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/core"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/rendering"
"github.com/go-chi/chi"
log "github.com/sirupsen/logrus"
)
const errorWithCauseContentType = "application/vnd.aws.lambda.error.cause+json"
const xrayErrorCauseHeaderName = "Lambda-Runtime-Function-XRay-Error-Cause"
const invalidErrorBodyMessage = "Invalid error body"
const (
contentTypeHeader = "Content-Type"
functionResponseModeHeader = "Lambda-Runtime-Function-Response-Mode"
)
type invocationErrorHandler struct {
registrationService core.RegistrationService
}
func (h *invocationErrorHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
appCtx := appctx.FromRequest(request)
server := appctx.LoadResponseSender(appCtx)
if server == nil {
log.Panic("Invalid state, cannot access interop server")
}
runtime := h.registrationService.GetRuntime()
if err := runtime.InvocationErrorResponse(); err != nil {
log.Warn(err)
rendering.RenderForbiddenWithTypeMsg(writer, request, rendering.ErrorTypeInvalidStateTransition, StateTransitionFailedForRuntimeMessageFormat,
runtime.GetState().Name(), core.RuntimeInvocationErrorResponseStateName, err)
return
}
errorType := fatalerror.GetValidRuntimeOrFunctionErrorType(h.getErrorType(request.Header))
var errorCause json.RawMessage
var errorBody []byte
var contentType string
var err error
switch request.Header.Get(contentTypeHeader) {
case errorWithCauseContentType:
errorBody, errorCause, err = h.getErrorBodyForErrorCauseContentType(request)
contentType = "application/json"
if err != nil {
contentType = "application/octet-stream"
}
default:
errorBody, err = h.getErrorBody(request)
errorCause = h.getValidatedErrorCause(request.Header)
contentType = request.Header.Get(contentTypeHeader)
}
functionResponseMode := request.Header.Get(functionResponseModeHeader)
if err != nil {
log.WithError(err).Warn("Failed to parse error body")
}
headers := interop.InvokeResponseHeaders{
ContentType: contentType,
FunctionResponseMode: functionResponseMode,
}
response := &interop.ErrorInvokeResponse{
Headers: headers,
FunctionError: interop.FunctionError{Type: errorType},
Payload: errorBody,
}
if err := server.SendErrorResponse(chi.URLParam(request, "awsrequestid"), response); err != nil {
rendering.RenderInteropError(writer, request, err)
return
}
appctx.StoreInvokeErrorTraceData(appCtx, &interop.InvokeErrorTraceData{ErrorCause: errorCause})
if err := runtime.ResponseSent(); err != nil {
log.Panic(err)
}
rendering.RenderAccepted(writer, request)
}
func (h *invocationErrorHandler) getErrorType(headers http.Header) string {
return headers.Get("Lambda-Runtime-Function-Error-Type")
}
func (h *invocationErrorHandler) getErrorBody(request *http.Request) ([]byte, error) {
errorBody, err := io.ReadAll(request.Body)
if err != nil {
return nil, fmt.Errorf("error reading request body: %s", err)
}
return errorBody, nil
}
func (h *invocationErrorHandler) getValidatedErrorCause(headers http.Header) json.RawMessage {
errorCauseHeader := headers.Get(xrayErrorCauseHeaderName)
if len(errorCauseHeader) == 0 {
return nil
}
errorCauseJSON := json.RawMessage(errorCauseHeader)
validErrorCauseJSON, err := model.ValidatedErrorCauseJSON(errorCauseJSON)
if err != nil {
log.WithError(err).Error("errorCause validation error")
return nil
}
return validErrorCauseJSON
}
func (h *invocationErrorHandler) getErrorBodyForErrorCauseContentType(request *http.Request) ([]byte, json.RawMessage, error) {
errorBody, err := io.ReadAll(request.Body)
if err != nil {
return nil, nil, fmt.Errorf("error reading request body: %s", err)
}
parsedError, err := newErrorWithCauseRequest(errorBody)
if err != nil {
errResponse, _ := json.Marshal(invalidErrorBodyMessage)
return errResponse, nil, fmt.Errorf("error parsing request body: %s, request.Body: %s", err, errorBody)
}
filteredError, err := parsedError.getInvokeErrorResponse()
return filteredError, parsedError.getValidatedXRayCause(), err
}
// NewInvocationErrorHandler returns a new instance of http handler
// for serving /runtime/invocation/{awsrequestid}/error.
func NewInvocationErrorHandler(registrationService core.RegistrationService) http.Handler {
return &invocationErrorHandler{
registrationService: registrationService,
}
}