Module: Appfuel::Application::Dispatcher

Included in:
Root
Defined in:
lib/appfuel/application/dispatcher.rb

Instance Method Summary collapse

Instance Method Details

#default_exception_handlerObject



39
40
41
42
43
44
# File 'lib/appfuel/application/dispatcher.rb', line 39

def default_exception_handler
  ->(e, logger = nil) {
    logger ||= Logger.new(STDOUT)
    logger.error "#{e.class.to_s} #{e.to_s} #{e.backtrace.join("\n")}"
  }
end

#dispatch(request, container) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/appfuel/application/dispatcher.rb', line 5

def dispatch(request, container)
  begin
    container[:feature_initializer].call(request.feature, container)
    action   = container[:action_loader].call(request.namespace, container)
    response = action.run(request.inputs)
  rescue Appfuel::Handler::HandlerFailure => e
    error_handler_key = :dispatcher_action_error_handler
    response = handle_exception(error_handler_key, e, container)
  rescue => e
    error_handler_key = :dispatcher_general_error_handler
    response = handle_exception(error_handler_key, e, container)
  end

  response
end

#handle_exception(error_handler_key, e, container) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/appfuel/application/dispatcher.rb', line 21

def handle_exception(error_handler_key, e, container)
  logger = container[:logger]
  load_error_handler(error_handler_key, container).call(e, logger)
  if error_handler_key == :dispatcher_action_error_handler
    return e.response
  end

  Appfuel::ResponseHandler.new.error(e)
end

#load_error_handler(key, container) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/appfuel/application/dispatcher.rb', line 31

def load_error_handler(key, container)
  handler = default_exception_handler
  if container.key?(key)
    handler = container[key]
  end
  handler
end