Class: Sanford::ErrorHandler

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

Constant Summary collapse

STANDARD_ERROR_CLASSES =

these are standard error classes that we rescue and run through any configured error procs; use the same standard error classes that dat-worker-pool rescues

DatWorkerPool::Worker::STANDARD_ERROR_CLASSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, context_hash) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



15
16
17
18
19
# File 'lib/sanford/error_handler.rb', line 15

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/sanford/error_handler.rb', line 13

def context
  @context
end

#error_procsObject (readonly)

Returns the value of attribute error_procs.



13
14
15
# File 'lib/sanford/error_handler.rb', line 13

def error_procs
  @error_procs
end

#exceptionObject (readonly)

Returns the value of attribute exception.



13
14
15
# File 'lib/sanford/error_handler.rb', line 13

def exception
  @exception
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.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sanford/error_handler.rb', line 27

def run
  response = nil
  @error_procs.each do |error_proc|
    result = nil
    begin
      result = error_proc.call(@exception, @context)
    rescue *STANDARD_ERROR_CLASSES => proc_exception
      @exception = proc_exception
    end
    response ||= response_from_proc(result)
  end
  response || response_from_exception(@exception)
end