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

Commit

Permalink
Merge pull request #72 from triggermesh/empty-responses
Browse files Browse the repository at this point in the history
Empty responses support
  • Loading branch information
tzununbekov committed Nov 5, 2021
2 parents e9bab68 + c38e692 commit d891558
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion node10/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async function invokeResponse(result, context) {
const res = await request({
method: 'POST',
path: `${RUNTIME_PATH}/invocation/${context.awsRequestId}/response`,
body: JSON.stringify(result),
body: result === null ? '' : JSON.stringify(result),
})
if (res.statusCode !== 202) {
throw new Error(`Unexpected /invocation/response response: ${JSON.stringify(res)}`)
Expand Down
3 changes: 2 additions & 1 deletion python37/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ def handle_event_request(lambda_runtime_client, request_handler, invoke_id, even
context = LambdaContext(invoke_id, client_context, cloudevents_context, cognito_identity, epoch_deadline_time_in_ms, invoked_function_arn)
json_input = try_or_raise(lambda: json.loads(event_body.decode()), "Unable to parse input as json")
result = request_handler(json_input, context)
result = try_or_raise(lambda: to_json(result), "An error occurred during JSON serialization of response")
if result is not None:
result = try_or_raise(lambda: to_json(result), "An error occurred during JSON serialization of response")
except FaultException as e:
error_result = make_error(e.msg, None, None)
error_result = to_json(error_result)
Expand Down
2 changes: 1 addition & 1 deletion ruby25/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ENV LAMBDA_TASK_ROOT "/opt"

COPY --from=downloader /opt/aws-custom-runtime /opt/
COPY --from=lambda /var/runtime/lib /opt
COPY lambda_context.rb /opt/
COPY lambda_context.rb lambda_handler.rb /opt/

RUN mv /opt/runtime.rb /opt/bootstrap
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|'
42 changes: 42 additions & 0 deletions ruby25/lambda_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

class LambdaHandler
attr_reader :handler_file_name, :handler_method_name

def initialize(env_handler:)
handler_split = env_handler.split('.')
if handler_split.size == 2
@handler_file_name, @handler_method_name = handler_split
elsif handler_split.size == 3
@handler_file_name, @handler_class, @handler_method_name = handler_split
else
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.")
end
end

def call_handler(request:, context:)
begin
opts = {
event: request,
context: context
}
if @handler_class
response = Kernel.const_get(@handler_class).send(@handler_method_name, opts)
else
response = __send__(@handler_method_name, opts)
end
# serialization can be a part of user code
response.nil? ? response : AwsLambda::Marshaller.marshall_response(response)
rescue NoMethodError => e
# This is a special case of standard error that we want to hard-fail for
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
rescue NameError => e
# This is a special case error that we want to wrap
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
rescue StandardError => e
raise LambdaErrors::LambdaHandlerError.new(e)
rescue Exception => e
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
end
end
end

0 comments on commit d891558

Please sign in to comment.