Class: LambdaHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_lambda_ric/lambda_handler.rb

Overview

frozen_string_literal: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_handler:) ⇒ LambdaHandler

Returns a new instance of LambdaHandler.



9
10
11
12
13
14
15
16
17
18
# File 'lib/aws_lambda_ric/lambda_handler.rb', line 9

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

Instance Attribute Details

#handler_file_nameObject (readonly)

Returns the value of attribute handler_file_name.



7
8
9
# File 'lib/aws_lambda_ric/lambda_handler.rb', line 7

def handler_file_name
  @handler_file_name
end

#handler_method_nameObject (readonly)

Returns the value of attribute handler_method_name.



7
8
9
# File 'lib/aws_lambda_ric/lambda_handler.rb', line 7

def handler_method_name
  @handler_method_name
end

Instance Method Details

#call_handler(request:, context:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aws_lambda_ric/lambda_handler.rb', line 20

def call_handler(request:, context:)
  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
  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