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]
CONFIGURATION_SECTIONS =
{
    send_email:  false,  # should email be sent?
    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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_name, configuration) ⇒ ExceptionDescription

Returns a new instance of ExceptionDescription.



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

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_email  = @configuration[:send_email]
  @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, 'i') ] }]

  !@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.



12
13
14
# File 'lib/exception_handling/exception_description.rb', line 12

def filter_name
  @filter_name
end

#metric_nameObject (readonly)

Returns the value of attribute metric_name.



12
13
14
# File 'lib/exception_handling/exception_description.rb', line 12

def metric_name
  @metric_name
end

#notesObject (readonly)

Returns the value of attribute notes.



12
13
14
# File 'lib/exception_handling/exception_description.rb', line 12

def notes
  @notes
end

#send_emailObject (readonly)

Returns the value of attribute send_email.



12
13
14
# File 'lib/exception_handling/exception_description.rb', line 12

def send_email
  @send_email
end

#send_metricObject (readonly)

Returns the value of attribute send_metric.



12
13
14
# File 'lib/exception_handling/exception_description.rb', line 12

def send_metric
  @send_metric
end

Instance Method Details

#exception_dataObject



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

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

#match?(exception_data) ⇒ Boolean

Returns:

  • (Boolean)


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

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