Class: ApiErrorHandler::ErrorReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/api_error_handler/error_reporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ ErrorReporter

Returns a new instance of ErrorReporter.



8
9
10
# File 'lib/api_error_handler/error_reporter.rb', line 8

def initialize(strategy)
  @strategy = strategy
end

Instance Method Details

#report(error, error_id: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_error_handler/error_reporter.rb', line 12

def report(error, error_id: nil)
  if @strategy.nil?
    true
  elsif @strategy.instance_of?(Proc)
    @strategy.call(error, error_id)
  elsif @strategy == :honeybadger
    raise_dependency_error(missing_constant: "Honeybadger") unless defined?(Honeybadger)

    context = error_id ? { error_id: error_id } : {}
    Honeybadger.notify(error, context: context)
  elsif @strategy == :raven
    raise_dependency_error(missing_constant: "Raven") unless defined?(Raven)

    extra = error_id ? { error_id: error_id } : {}
    Raven.capture_exception(error, extra: extra)
  elsif @strategy == :sentry
    raise_dependency_error(missing_constant: "Sentry") unless defined?(Sentry)

    extra = error_id ? { error_id: error_id } : {}
    Sentry.capture_exception(error, extra: extra)
  else
    raise(
      InvalidOptionError,
      "`#{@strategy.inspect}` is an invalid argument for the `:error_id` option."
    )
  end
end