Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit d891558

Browse files
authored
Merge pull request #72 from triggermesh/empty-responses
Empty responses support
2 parents e9bab68 + c38e692 commit d891558

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

node10/bootstrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async function invokeResponse(result, context) {
123123
const res = await request({
124124
method: 'POST',
125125
path: `${RUNTIME_PATH}/invocation/${context.awsRequestId}/response`,
126-
body: JSON.stringify(result),
126+
body: result === null ? '' : JSON.stringify(result),
127127
})
128128
if (res.statusCode !== 202) {
129129
throw new Error(`Unexpected /invocation/response response: ${JSON.stringify(res)}`)

python37/bootstrap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def handle_event_request(lambda_runtime_client, request_handler, invoke_id, even
149149
context = LambdaContext(invoke_id, client_context, cloudevents_context, cognito_identity, epoch_deadline_time_in_ms, invoked_function_arn)
150150
json_input = try_or_raise(lambda: json.loads(event_body.decode()), "Unable to parse input as json")
151151
result = request_handler(json_input, context)
152-
result = try_or_raise(lambda: to_json(result), "An error occurred during JSON serialization of response")
152+
if result is not None:
153+
result = try_or_raise(lambda: to_json(result), "An error occurred during JSON serialization of response")
153154
except FaultException as e:
154155
error_result = make_error(e.msg, None, None)
155156
error_result = to_json(error_result)

ruby25/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ENV LAMBDA_TASK_ROOT "/opt"
1515

1616
COPY --from=downloader /opt/aws-custom-runtime /opt/
1717
COPY --from=lambda /var/runtime/lib /opt
18-
COPY lambda_context.rb /opt/
18+
COPY lambda_context.rb lambda_handler.rb /opt/
1919

2020
RUN mv /opt/runtime.rb /opt/bootstrap
2121
RUN sed -i /opt/lambda_server.rb -e 's|http://127.0.0.1:9001/2018-06-01|http://127.0.0.1/2018-06-01|'

ruby25/lambda_handler.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
class LambdaHandler
4+
attr_reader :handler_file_name, :handler_method_name
5+
6+
def initialize(env_handler:)
7+
handler_split = env_handler.split('.')
8+
if handler_split.size == 2
9+
@handler_file_name, @handler_method_name = handler_split
10+
elsif handler_split.size == 3
11+
@handler_file_name, @handler_class, @handler_method_name = handler_split
12+
else
13+
raise ArgumentError.new("Invalid handler #{handler_split}, must be of form FILENAME.METHOD or FILENAME.CLASS.METHOD where FILENAME corresponds with an existing Ruby source file FILENAME.rb, CLASS is an optional module/class namespace and METHOD is a callable method. If using CLASS, METHOD must be a class-level method.")
14+
end
15+
end
16+
17+
def call_handler(request:, context:)
18+
begin
19+
opts = {
20+
event: request,
21+
context: context
22+
}
23+
if @handler_class
24+
response = Kernel.const_get(@handler_class).send(@handler_method_name, opts)
25+
else
26+
response = __send__(@handler_method_name, opts)
27+
end
28+
# serialization can be a part of user code
29+
response.nil? ? response : AwsLambda::Marshaller.marshall_response(response)
30+
rescue NoMethodError => e
31+
# This is a special case of standard error that we want to hard-fail for
32+
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
33+
rescue NameError => e
34+
# This is a special case error that we want to wrap
35+
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
36+
rescue StandardError => e
37+
raise LambdaErrors::LambdaHandlerError.new(e)
38+
rescue Exception => e
39+
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)