Class: ExceptionHandling::ExceptionDescription

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_handling/exception_description.rb

Constant Summary collapse

MATCH_SECTIONS =
[:error, :request, :session, :environment, :backtrace, :event_response].freeze
CONFIGURATION_SECTIONS =
{
  send_to_honeybadger: false,  # should be sent to honeybadger?
  send_metric: true,   # should the metric be sent.
  metric_name: nil,    # Will be derived from section name if not passed
  notes: nil     # Will be included in exception email if set, used to keep notes and relevant links
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_name, configuration) ⇒ ExceptionDescription

Returns a new instance of ExceptionDescription.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/exception_handling/exception_description.rb', line 16

def initialize(filter_name, configuration)
  @filter_name = filter_name

  invalid_sections = configuration.except(*(CONFIGURATION_SECTIONS.keys + MATCH_SECTIONS))
  invalid_sections.empty? or raise ArgumentError, "Unknown section: #{invalid_sections.keys.join(',')}"

  @configuration = CONFIGURATION_SECTIONS.merge(configuration)
  @send_to_honeybadger = @configuration[:send_to_honeybadger]
  @send_metric = @configuration[:send_metric]
  @metric_name = (@configuration[:metric_name] || @filter_name).to_s.gsub(" ", "_")
  @notes       = @configuration[:notes]

  regex_config = @configuration.reject { |k, v| k.in?(CONFIGURATION_SECTIONS.keys) || v.blank? }

  @regexes = Hash[regex_config.map { |section, regex| [section, Regexp.new(regex, Regexp::IGNORECASE | Regexp::MULTILINE)] }]

  !@regexes.empty? or raise ArgumentError, "Filter #{filter_name} has all blank regexes: #{configuration.inspect}"
end

Instance Attribute Details

#filter_nameObject (readonly)

Returns the value of attribute filter_name.



14
15
16
# File 'lib/exception_handling/exception_description.rb', line 14

def filter_name
  @filter_name
end

#metric_nameObject (readonly)

Returns the value of attribute metric_name.



14
15
16
# File 'lib/exception_handling/exception_description.rb', line 14

def metric_name
  @metric_name
end

#notesObject (readonly)

Returns the value of attribute notes.



14
15
16
# File 'lib/exception_handling/exception_description.rb', line 14

def notes
  @notes
end

#send_metricObject (readonly)

Returns the value of attribute send_metric.



14
15
16
# File 'lib/exception_handling/exception_description.rb', line 14

def send_metric
  @send_metric
end

#send_to_honeybadgerObject (readonly)

Returns the value of attribute send_to_honeybadger.



14
15
16
# File 'lib/exception_handling/exception_description.rb', line 14

def send_to_honeybadger
  @send_to_honeybadger
end

Instance Method Details

#exception_dataObject



35
36
37
38
39
40
41
# File 'lib/exception_handling/exception_description.rb', line 35

def exception_data
  {
    "send_metric" => send_metric,
    "metric_name" => metric_name,
    "notes" => notes
  }
end

#match?(exception_data) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/exception_handling/exception_description.rb', line 43

def match?(exception_data)
  @regexes.all? do |section, regex|
    case target = exception_data[section.to_s] || exception_data[section]
    when String
      regex =~ target
    when Array
      target.any? { |row| row =~ regex }
    when Hash
      target[:to_s] =~ regex
    when NilClass
      false
    else
      raise "Unexpected class #{exception_data[section].class.name}"
    end
  end
end