Skip to content

Commit fd8c76c

Browse files
authored
feat: add implementation of init and invocation errors (#10)
1 parent 14cf635 commit fd8c76c

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

runtime/bootstrap

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
#!/bin/sh
22

33
# https://docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html
4+
# https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
45

5-
# set -euo pipefail
66
set -eo pipefail
77

8+
# Function to send initialization error
9+
send_init_error() {
10+
local error_message="$1"
11+
curl -sS -X POST \
12+
"http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error" \
13+
-H "Content-Type: application/json" \
14+
-d '{"errorMessage": "'"$error_message"'", "errorType": "Runtime.InitError"}'
15+
exit 1
16+
}
17+
18+
# Function to send invocation error
19+
send_invocation_error() {
20+
local request_id="$1"
21+
local error_message="$2"
22+
curl -sS -X POST \
23+
"http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${request_id}/error" \
24+
-H "Content-Type: application/json" \
25+
-d '{"errorMessage": "'"$error_message"'", "errorType": "Runtime.HandlerError"}'
26+
}
27+
828
# Initialization - load function handler
9-
source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"
29+
if [ -z "$_HANDLER" ]; then
30+
send_init_error "Handler not specified"
31+
fi
32+
33+
HANDLER_FILE="$(echo $_HANDLER | cut -d. -f1).sh"
34+
HANDLER_FUNC="$(echo $_HANDLER | cut -d. -f2)"
35+
36+
if [ ! -f "$LAMBDA_TASK_ROOT/$HANDLER_FILE" ]; then
37+
send_init_error "Handler file not found: $HANDLER_FILE"
38+
fi
39+
40+
source "$LAMBDA_TASK_ROOT/$HANDLER_FILE" || send_init_error "Failed to source handler file: $HANDLER_FILE"
41+
42+
# Check if handler function exists
43+
if ! type "$HANDLER_FUNC" >/dev/null 2>&1; then
44+
send_init_error "Handler function not found: $HANDLER_FUNC"
45+
fi
1046

1147
# Processing
1248
while true
@@ -18,12 +54,17 @@ do
1854
# Extract request ID by scraping response headers received above
1955
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
2056

21-
# Run the handler function from the script
22-
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
23-
24-
25-
echo "$RESPONSE" | curl -sS -X POST \
26-
"http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${REQUEST_ID}/response" \
27-
-H "Content-Type: application/json" \
28-
-d @-
57+
# Run the handler function from the script with error handling
58+
if RESPONSE=$("$HANDLER_FUNC" "$EVENT_DATA" 2>&1); then
59+
# Success - send response
60+
echo "$RESPONSE" | curl -sS -X POST \
61+
"http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${REQUEST_ID}/response" \
62+
-H "Content-Type: application/json" \
63+
-d @-
64+
else
65+
# Error - send error response
66+
send_invocation_error "$REQUEST_ID" "Handler function failed: $RESPONSE"
67+
fi
68+
69+
rm -f "$HEADERS"
2970
done

0 commit comments

Comments
 (0)