forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initerror.go
85 lines (69 loc) · 2.97 KB
/
initerror.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package handler
import (
"encoding/json"
"io"
"net/http"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/appctx"
"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/core"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/rendering"
log "github.com/sirupsen/logrus"
)
type initErrorHandler struct {
registrationService core.RegistrationService
}
func (h *initErrorHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
appCtx := appctx.FromRequest(request)
interopServer := appctx.LoadInteropServer(appCtx)
if interopServer == nil {
log.Panic("Invalid state, cannot access interop server")
}
errorType := fatalerror.GetValidRuntimeOrFunctionErrorType(request.Header.Get("Lambda-Runtime-Function-Error-Type"))
fnError := interop.FunctionError{Type: errorType}
errorBody, err := io.ReadAll(request.Body)
if err != nil {
log.WithError(err).Warn("Failed to read error body")
}
headers := interop.InvokeResponseHeaders{ContentType: determineJSONContentType(errorBody)}
response := &interop.ErrorInvokeResponse{Headers: headers, FunctionError: fnError, Payload: errorBody}
runtime := h.registrationService.GetRuntime()
// remove once Languages team change the endpoint to /restore/error
// when an exception is throw while executing the restore hooks
if runtime.GetState() == runtime.RuntimeRestoringState {
if err := runtime.RestoreError(fnError); err != nil {
log.Warn(err)
rendering.RenderForbiddenWithTypeMsg(writer, request, rendering.ErrorTypeInvalidStateTransition, StateTransitionFailedForRuntimeMessageFormat,
runtime.GetState().Name(), core.RuntimeRestoreErrorStateName, err)
return
}
appctx.StoreInvokeErrorTraceData(appCtx, &interop.InvokeErrorTraceData{})
rendering.RenderAccepted(writer, request)
return
}
if err := runtime.InitError(); err != nil {
log.Warn(err)
rendering.RenderForbiddenWithTypeMsg(writer, request, rendering.ErrorTypeInvalidStateTransition, StateTransitionFailedForRuntimeMessageFormat,
runtime.GetState().Name(), core.RuntimeInitErrorStateName, err)
return
}
if err := interopServer.SendInitErrorResponse(response); err != nil {
rendering.RenderInteropError(writer, request, err)
return
}
appctx.StoreInvokeErrorTraceData(appCtx, &interop.InvokeErrorTraceData{})
rendering.RenderAccepted(writer, request)
}
// NewInitErrorHandler returns a new instance of http handler
// for serving /runtime/init/error.
func NewInitErrorHandler(registrationService core.RegistrationService) http.Handler {
return &initErrorHandler{registrationService: registrationService}
}
func determineJSONContentType(body []byte) string {
if json.Valid(body) {
return "application/json"
}
return "application/octet-stream"
}