Class: Waw::ErrorHandler

Inherits:
Controller show all
Defined in:
lib/waw/controllers/error_handler.rb,
lib/waw/controllers/error/backtrace.rb

Defined Under Namespace

Classes: Backtrace

Constant Summary

Constants included from EnvironmentUtils

Waw::EnvironmentUtils::DEPRECATED_MSG

Instance Method Summary collapse

Methods inherited from Controller

#execute

Methods included from Rack::Delegator

#_visit, #delegate, #find_rack_app, #find_url_of, #has_delegate?, #is_delegate?, #visit

Methods included from EnvironmentUtils

#env, #session_get, #session_has_key?, #session_set, #session_unset

Methods included from ScopeUtils

#config, #find_kernel_context, #logger, #params, #rack_env, #real_session, #request, #resources, #response, #root_folder, #session

Constructor Details

#initialize(app) ⇒ ErrorHandler

Creates an error handler rack application



6
7
8
9
# File 'lib/waw/controllers/error_handler.rb', line 6

def initialize(app)
  @app = app
  @validators, @handlers = [], []
end

Instance Method Details

#add_error_handler(*args, &block) ⇒ Object Also known as: push

Adds an error handler. The method accepts the following argument variants:

add_error_handler{|k,ex| ... }
add_error_handler(SomeValidator){|k,ex| ... }
add_error_handler(SomeHandler)
add_error_handler(SomeValidator, SomeHandler)


27
28
29
30
31
# File 'lib/waw/controllers/error_handler.rb', line 27

def add_error_handler(*args, &block)
  validator, handler = handle_args(args, &block)
  @validators << validator
  @handlers << handler
end

#call(env) ⇒ Object

Handles a call



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/waw/controllers/error_handler.rb', line 48

def call(env)
  @app.call(env)
rescue Exception => ex
  kernel = find_kernel_context
  @validators.each_with_index do |validator, i|
    if (validator.nil? or (validator===ex))
      result = @handlers[i].call(kernel, ex)
      return result if seems_valid_result?(result)
    end
  end
  raise ex
end

#handle_args(args, &block) ⇒ Object

Handles arguments of push and unshift



12
13
14
15
16
17
# File 'lib/waw/controllers/error_handler.rb', line 12

def handle_args(args, &block)
  validator, handler = args
  handler = block if handler.nil?
  validator, handler = nil, validator if handler.nil?
  [validator, handler]
end

#seems_valid_result?(result) ⇒ Boolean

Checks if a result seems a valid rack application result

Returns:



43
44
45
# File 'lib/waw/controllers/error_handler.rb', line 43

def seems_valid_result?(result)
  not(result.nil?) and Array===result and result.size==3
end

#unshift(*args, &block) ⇒ Object

Same as add_error_handler but adds it at the begining of the chain



36
37
38
39
40
# File 'lib/waw/controllers/error_handler.rb', line 36

def unshift(*args, &block)
  validator, handler = handle_args(args, &block)
  @validators.unshift(validator)
  @handlers.unshift(handler)
end