Class: Pwwka::ErrorHandlers::Chain

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pwwka/error_handlers/chain.rb

Overview

Given a chain of error handlers, calls them until either one returns false/aborts or we exhaust the chain of handlers

Constant Summary

Constants included from Logging

Logging::LEVELS

Instance Method Summary collapse

Methods included from Logging

#logf, #logger

Constructor Details

#initialize(default_handler_chain = []) ⇒ Chain

Returns a new instance of Chain.



7
8
9
# File 'lib/pwwka/error_handlers/chain.rb', line 7

def initialize(default_handler_chain=[])
  @error_handlers = default_handler_chain
end

Instance Method Details

#handle_error(message_handler_klass, receiver, queue_name, payload, delivery_info, exception) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pwwka/error_handlers/chain.rb', line 10

def handle_error(message_handler_klass,receiver,queue_name,payload,delivery_info,exception)
  logf "Error Processing Message in %{message_handler_klass} due to %{exception} from payload '%{payload}'", at: :error, message_handler_klass: message_handler_klass, exception: exception.message, payload: payload
  if message_handler_klass.respond_to?(:error_handler)
    @error_handlers.unshift(message_handler_klass.send(:error_handler))
  end
  @error_handlers.reduce(true) { |keep_going,error_handler|
    begin
      logf "%{error_handler_class} is being evaluated as part of pwwka's error-handling chain", error_handler_class: error_handler
      if keep_going
        keep_going = error_handler.new.handle_error(receiver,queue_name,payload,delivery_info,exception)
        if keep_going
          logf "%{error_handler_class} has asked to continue pwwka's error-handling chain", error_handler_class: error_handler
        else
          logf "%{error_handler_class} has halted pwwka's error-handling chain", error_handler_class: error_handler
        end
      else
        logf "Skipping %{error_handler_class} as we were asked to abort pwwka's error-handling chain", error_handler_class: error_handler
      end
      keep_going
    rescue StandardError => exception
      logf "'%{error_handler_class}' aborting due to unhandled exception '%{exception}'", at: :fatal, error_handler_class: error_handler, exception: exception
      false
    end
  }
end