Class: Exceptify::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/exceptify/configuration.rb

Constant Summary collapse

DEFAULT_IGNORED_EXCEPTIONS =
%w[
  ActiveRecord::RecordNotFound Mongoid::Errors::DocumentNotFound AbstractController::ActionNotFound
  ActionController::RoutingError ActionController::UnknownFormat ActionController::UrlGenerationError
  ActionDispatch::Http::MimeNegotiation::InvalidType Rack::Utils::InvalidParameterError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: Logger.new($stdout), ignored_exceptions: DEFAULT_IGNORED_EXCEPTIONS, notifier_registry: NotifierRegistry.new, error_grouping_cache: nil, fallback_cache_store: ActiveSupport::Cache::MemoryStore.new) ⇒ Configuration

Returns a new instance of Configuration.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/exceptify/configuration.rb', line 28

def initialize(
  logger: Logger.new($stdout),
  ignored_exceptions: DEFAULT_IGNORED_EXCEPTIONS,
  notifier_registry: NotifierRegistry.new,
  error_grouping_cache: nil,
  fallback_cache_store: ActiveSupport::Cache::MemoryStore.new
)
  @logger = logger
  @ignored_exceptions = ignored_exceptions.dup
  @testing_mode = false
  @error_grouping = false
  @error_grouping_period = 5.minutes
  @notification_trigger = nil
  @error_grouping_cache = error_grouping_cache
  @fallback_cache_store = fallback_cache_store
  @notifier_registry = notifier_registry
  @ignores = []
  @by_notifier_ignores = {}
end

Instance Attribute Details

#error_groupingObject

Returns the value of attribute error_grouping.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def error_grouping
  @error_grouping
end

#error_grouping_cacheObject

Returns the value of attribute error_grouping_cache.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def error_grouping_cache
  @error_grouping_cache
end

#error_grouping_periodObject

Returns the value of attribute error_grouping_period.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def error_grouping_period
  @error_grouping_period
end

#fallback_cache_storeObject

Returns the value of attribute fallback_cache_store.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def fallback_cache_store
  @fallback_cache_store
end

#ignored_exceptionsObject

Returns the value of attribute ignored_exceptions.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def ignored_exceptions
  @ignored_exceptions
end

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def logger
  @logger
end

#notification_triggerObject

Returns the value of attribute notification_trigger.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def notification_trigger
  @notification_trigger
end

#notifier_registryObject (readonly)

Returns the value of attribute notifier_registry.



20
21
22
# File 'lib/exceptify/configuration.rb', line 20

def notifier_registry
  @notifier_registry
end

#testing_modeObject

Returns the value of attribute testing_mode.



11
12
13
# File 'lib/exceptify/configuration.rb', line 11

def testing_mode
  @testing_mode
end

Instance Method Details

#clear_ignore_conditions!Object



109
110
111
112
# File 'lib/exceptify/configuration.rb', line 109

def clear_ignore_conditions!
  ignores.clear
  by_notifier_ignores.clear
end

#copyObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/exceptify/configuration.rb', line 48

def copy
  self.class.new(
    logger: logger,
    ignored_exceptions: ignored_exceptions,
    notifier_registry: notifier_registry.copy,
    error_grouping_cache: error_grouping_cache,
    fallback_cache_store: fallback_cache_store
  ).tap do |configuration|
    configuration.testing_mode = testing_mode
    configuration.error_grouping = error_grouping
    configuration.error_grouping_period = error_grouping_period
    configuration.notification_trigger = notification_trigger
    ignores.each { |condition| configuration.ignore_if(&condition) }
    by_notifier_ignores.each { |notifier, condition| configuration.ignore_notifier_if(notifier, &condition) }
  end
end

#error_count(error_key) ⇒ Object



145
146
147
# File 'lib/exceptify/configuration.rb', line 145

def error_count(error_key)
  error_grouping_service.error_count(error_key)
end

#group_error!(exception, options) ⇒ Object



153
154
155
# File 'lib/exceptify/configuration.rb', line 153

def group_error!(exception, options)
  error_grouping_service.group_error!(exception, options)
end

#ignore_crawlers(crawlers) ⇒ Object



103
104
105
106
107
# File 'lib/exceptify/configuration.rb', line 103

def ignore_crawlers(crawlers)
  ignore_if do |_exception, opts|
    opts.key?(:env) && from_crawler(opts[:env], crawlers)
  end
end

#ignore_if(&block) ⇒ Object



95
96
97
# File 'lib/exceptify/configuration.rb', line 95

def ignore_if(&block)
  ignores << block
end

#ignore_notifier_if(notifier, &block) ⇒ Object



99
100
101
# File 'lib/exceptify/configuration.rb', line 99

def ignore_notifier_if(notifier, &block)
  by_notifier_ignores[notifier] = block
end

#ignored?(exception, options) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
# File 'lib/exceptify/configuration.rb', line 114

def ignored?(exception, options)
  ignores.any? { |condition| condition.call(exception, options) }
rescue Exception => e # standard:disable Lint/RescueException
  raise e if testing_mode

  logger.warn(
    "An error occurred when evaluating an ignore condition. #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
  )
  false
end

#ignored_exception?(ignore_array, exception) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
# File 'lib/exceptify/configuration.rb', line 139

def ignored_exception?(ignore_array, exception)
  all_ignored_exceptions = (Array(ignored_exceptions) + Array(ignore_array)).map(&:to_s)
  exception_ancestors = exception.singleton_class.ancestors.map(&:to_s)
  !(all_ignored_exceptions & exception_ancestors).empty?
end

#notifier_ignored?(exception, options, notifier:) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/exceptify/configuration.rb', line 125

def notifier_ignored?(exception, options, notifier:)
  return false unless by_notifier_ignores.key?(notifier)

  by_notifier_ignores[notifier].call(exception, options)
rescue Exception => e # standard:disable Lint/RescueException
  raise e if testing_mode

  logger.warn(<<~"MESSAGE")
    An error occurred when evaluating a by-notifier ignore condition. #{e.class}: #{e.message}
    #{e.backtrace.join("\n")}
  MESSAGE
  false
end

#notifiersObject



91
92
93
# File 'lib/exceptify/configuration.rb', line 91

def notifiers
  notifier_registry.names
end

#register_notifier(name, notifier_or_options) ⇒ Object Also known as: add_notifier



78
79
80
# File 'lib/exceptify/configuration.rb', line 78

def register_notifier(name, notifier_or_options)
  notifier_registry.register(name, notifier_or_options)
end

#registered_notifier(name) ⇒ Object



87
88
89
# File 'lib/exceptify/configuration.rb', line 87

def registered_notifier(name)
  notifier_registry.fetch(name)
end

#reset!Object



65
66
67
68
69
70
71
72
# File 'lib/exceptify/configuration.rb', line 65

def reset!
  notifier_registry.clear
  clear_ignore_conditions!
  self.error_grouping = false
  self.notification_trigger = nil
  self.error_grouping_cache = nil
  fallback_cache_store.clear if fallback_cache_store.respond_to?(:clear)
end

#save_error_count(error_key, count) ⇒ Object



149
150
151
# File 'lib/exceptify/configuration.rb', line 149

def save_error_count(error_key, count)
  error_grouping_service.save_error_count(error_key, count)
end

#send_notification?(exception, count) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/exceptify/configuration.rb', line 157

def send_notification?(exception, count)
  error_grouping_service.send_notification?(exception, count)
end

#testing_mode!Object



74
75
76
# File 'lib/exceptify/configuration.rb', line 74

def testing_mode!
  self.testing_mode = true
end

#unregister_notifier(name) ⇒ Object



83
84
85
# File 'lib/exceptify/configuration.rb', line 83

def unregister_notifier(name)
  notifier_registry.unregister(name)
end