class ChiliLogger::LoggingErrorHandler

class that handles errors when message broker can't be reached, etc…

Public Class Methods

new(fallback_name = nil, config = {}) click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 8
def initialize(fallback_name = nil, config = {})
  return if ChiliLogger.instance.deactivated

  @default = ChiliLogger::Values::Default.new
  config ||= {}
  validate_config(config)

  fallback_name ||= @default.fallback_broker
  fallback_broker_class = supported_fallback_brokers[fallback_name.to_sym]
  unsupported_fallback_broker_error unless fallback_broker_class

  @fallback_broker = fallback_broker_class.new(config)
end

Public Instance Methods

delete_unpublished_log(msg) click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 39
def delete_unpublished_log(msg)
  @fallback_broker.delete_message(msg)
end
fetch_unpublished_logs() click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 35
def fetch_unpublished_logs
  @fallback_broker.fetch_messages
end
handle_error(error, log = nil) click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 22
def handle_error(error, log = nil)
  return if ChiliLogger.instance.deactivated

  message = message(error, log)
  @fallback_broker.publish(message)
rescue StandardError => e
  puts 'There was a problem with both the Message Broker and the Fallback Broker simultaneously.
    To keep the application running and prevent downtime,
    ChiliLogger will ignore this errors and discard the log it was currently trying to publish.
    Please note that logs are being permanently lost.'
  puts e
end

Private Instance Methods

message(error, log) click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 50
def message(error, log)
  {
    error_type: error.class.name,
    error_message: error.message,
    log: log
  }
end
supported_fallback_brokers() click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 65
def supported_fallback_brokers
  {
    aws_sqs: AWS::SqsBroker
  }
end
unsupported_fallback_broker_error() click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 58
def unsupported_fallback_broker_error
  error_handlers = supported_fallback_brokers.keys.join(', ')
  unsupported_err = ":error_handler must be present and have one of the following values: #{error_handlers}"

  raise(ChiliLogger::ConfigError, unsupported_err)
end
validate_config(config) click to toggle source
# File lib/errors/logging_error_handler/logging_error_handler.rb, line 45
def validate_config(config)
  invalid_config_err = ':fallback_broker_config must be a hash'
  raise(ChiliLogger::ConfigError, invalid_config_err) unless [Hash, NilClass].include?(config.class)
end