Class: Otto::RouteHandlers::LambdaHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/otto/route_handlers/lambda.rb

Overview

Custom handler for lambda/proc definitions (future extension)

Instance Attribute Summary

Attributes inherited from BaseHandler

#otto_instance, #route_definition

Instance Method Summary collapse

Methods inherited from BaseHandler

#initialize

Constructor Details

This class inherits a constructor from Otto::RouteHandlers::BaseHandler

Instance Method Details

#call(env, extra_params = {}) ⇒ Object



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
43
44
# File 'lib/otto/route_handlers/lambda.rb', line 12

def call(env, extra_params = {})
  start_time = Otto::Utils.now_in_μs
  req = otto_instance ? otto_instance.request_class.new(env) : Otto::Request.new(env)
  res = otto_instance ? otto_instance.response_class.new : Otto::Response.new

  begin
    # Security: Lambda handlers require pre-configured procs from Otto instance
    # This prevents code injection via eval and maintains security
    handler_name    = route_definition.klass_name
    lambda_registry = otto_instance&.config&.dig(:lambda_handlers) || {}

    lambda_proc = lambda_registry[handler_name]
    unless lambda_proc.respond_to?(:call)
      raise ArgumentError, "Lambda handler '#{handler_name}' not found in registry or not callable"
    end

    result = lambda_proc.call(req, res, extra_params)

    handle_response(result, res, {
                      lambda: lambda_proc,
      request: req,
                    })
  rescue StandardError => e
    # Store handler context in env for centralized error handler
    handler_name = "Lambda[#{route_definition.klass_name}]"
    env['otto.handler'] = handler_name
    env['otto.handler_duration'] = Otto::Utils.now_in_μs - start_time

    raise e # Re-raise to let Otto's centralized error handler manage the response
  end

  res.finish
end