forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agentexiterror.go
65 lines (55 loc) · 2.82 KB
/
agentexiterror.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package handler
import (
"net/http"
"github.com/google/uuid"
"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/fatalerror"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/rendering"
log "github.com/sirupsen/logrus"
)
type agentExitErrorHandler struct {
registrationService core.RegistrationService
}
func (h *agentExitErrorHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
agentID, ok := request.Context().Value(AgentIDCtxKey).(uuid.UUID)
if !ok {
rendering.RenderInternalServerError(writer, request)
return
}
var errorType string
if errorType = request.Header.Get(LambdaAgentFunctionErrorType); errorType == "" {
log.Warnf("Invalid /extension/exit/error: missing %s header, agentID: %s", LambdaAgentFunctionErrorType, agentID)
rendering.RenderForbiddenWithTypeMsg(writer, request, errAgentMissingHeader, "%s not found", LambdaAgentFunctionErrorType)
return
}
if externalAgent, found := h.registrationService.FindExternalAgentByID(agentID); found {
if err := externalAgent.ExitError(errorType); err != nil {
log.Warnf("Failed to transition agent %s to ExitError state: %s, current state %s", externalAgent.String(), err, externalAgent.GetState().Name())
rendering.RenderForbiddenWithTypeMsg(writer, request, errAgentInvalidState, StateTransitionFailedForExtensionMessageFormat,
externalAgent.GetState().Name(), core.AgentExitedStateName, agentID.String(), err)
return
}
} else if internalAgent, found := h.registrationService.FindInternalAgentByID(agentID); found {
if err := internalAgent.ExitError(errorType); err != nil {
log.Warnf("Failed to transition agent %s to ExitError state: %s, current state %s", internalAgent.String(), err, internalAgent.GetState().Name())
rendering.RenderForbiddenWithTypeMsg(writer, request, errAgentInvalidState, StateTransitionFailedForExtensionMessageFormat,
internalAgent.GetState().Name(), core.AgentExitedStateName, agentID.String(), err)
return
}
} else {
log.Warnf("Unknown agent %s tried to call /extension/exit/error", agentID)
rendering.RenderForbiddenWithTypeMsg(writer, request, errAgentIdentifierUnknown, "Unknown "+LambdaAgentIdentifier)
return
}
appctx.StoreFirstFatalError(appctx.FromRequest(request), fatalerror.AgentExitError)
rendering.RenderAccepted(writer, request)
}
// NewAgentExitErrorHandler returns a new instance of http handler for serving /extension/exit/error
func NewAgentExitErrorHandler(registrationService core.RegistrationService) http.Handler {
return &agentExitErrorHandler{
registrationService: registrationService,
}
}