This repository was archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlambda_handler.rb
42 lines (39 loc) · 1.77 KB
/
lambda_handler.rb
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
# 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