Class: Deas::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/deas/error_handler.rb

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, context_hash) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



11
12
13
14
15
# File 'lib/deas/error_handler.rb', line 11

def initialize(exception, context_hash)
  @exception   = exception
  @context     = Context.new(context_hash)
  @error_procs = context_hash[:server_data].error_procs.reverse
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/deas/error_handler.rb', line 9

def context
  @context
end

#error_procsObject (readonly)

Returns the value of attribute error_procs.



9
10
11
# File 'lib/deas/error_handler.rb', line 9

def error_procs
  @error_procs
end

#exceptionObject (readonly)

Returns the value of attribute exception.



9
10
11
# File 'lib/deas/error_handler.rb', line 9

def exception
  @exception
end

Class Method Details

.run(*args) ⇒ Object



5
6
7
# File 'lib/deas/error_handler.rb', line 5

def self.run(*args)
  self.new(*args).run
end

Instance Method Details

#runObject

The exception that we are generating a response for can change in the case that the configured error proc raises an exception. If this occurs, a response will be generated for that exception, instead of the original one. This is designed to avoid “hidden” errors happening, this way the server will respond and log based on the last exception that occurred.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deas/error_handler.rb', line 23

def run
  @error_procs.inject(nil) do |response, error_proc|
    result = begin
      error_proc.call(@exception, @context)
    rescue StandardError => proc_exception
      @exception = proc_exception
      response   = nil # reset response
    end
    response || result
  end
end