Sha256: b8c9a638d1c5c9e90af021253343c3b14a161064a8ddfdcd069d20098f9c8afe

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require_relative "base_processor"

class Lam::Process
  class ControllerProcessor < Lam::Process::BaseProcessor
    def run
      # Use the handler value (ie: posts.create) to deduce the user's business
      # code to require and run.
      deducer = ProcessorDeducer.new(handler)
      path = deducer.controller[:path]
      code = deducer.controller[:code]

      begin
        require path  # require "app/controllers/posts_controller.rb"
        # Puts the return value of user's code to stdout because this is
        # what eventually gets used by API Gateway.
        # Explicitly using $stdout since puts redirected to $stderr.

        # result = PostsController.new(event, context).create
        result = instance_eval(code, path)

        # JSON.dump is pretty robust.  If it cannot dump the structure into a
        # json string, it just dumps it to a plain text string.
        $stdout.puts JSON.dump(result) # only place where we write to stdout.
      rescue Exception => e
        # Customize error message slightly so nodejs shim can process the
        # returned error message.
        # The "RubyError: " is a marker that the javascript shim scans for.
        $stderr.puts("RubyError: #{e.class}: #{e.message}") # js needs this as the first line
        backtrace = e.backtrace.map {|l| "  #{l}" }
        $stderr.puts(backtrace)
        # $stderr.puts("END OF RUBY OUTPUT")
        exit 1 # instead of re-raising to control the error backtrace output
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lam-0.1.2 lib/lam/process/controller_processor.rb
lam-0.1.1 lib/lam/process/controller_processor.rb
lam-0.0.1 lib/lam/process/controller_processor.rb