Module: Chatterbox::ExceptionNotification

Extended by:
ExceptionNotification
Included in:
ExceptionNotification
Defined in:
lib/chatterbox/exception_notification.rb,
lib/chatterbox/exception_notification/extracter.rb,
lib/chatterbox/exception_notification/presenter.rb,
lib/chatterbox/exception_notification/rails_extracter.rb

Defined Under Namespace

Classes: Extracter, Presenter, RailsExtracter

Instance Method Summary collapse

Instance Method Details

#configurationObject

Default configuration for ExceptionNotification

:ignore => array of exceptions to ignore by default
:use_ignore_list => whether to use the ignore list - 
  defaults to true, and can be overidden on per exception level

52
53
54
55
56
# File 'lib/chatterbox/exception_notification.rb', line 52

def configuration
  @configuration ||= OpenStruct.new(
    :ignore => default_ignored_exceptions,
    :use_ignore_list => true)
end

#configure {|configuration| ... } ⇒ Object

Configure ExceptionNotification ex:

Chatterbox::ExceptionNotification.configure do |config|
   config.ignore << MyOwnExceptionToIgnore
end

Yields:


63
64
65
# File 'lib/chatterbox/exception_notification.rb', line 63

def configure
  yield(configuration)
end

#default_ignored_exceptionsObject

Default exceptions that ExceptionNotification will ignore


68
69
70
71
72
# File 'lib/chatterbox/exception_notification.rb', line 68

def default_ignored_exceptions
  ['ActiveRecord::RecordNotFound', 'ActionController::RoutingError',
   'ActionController::InvalidAuthenticityToken', 'ActionController::UnknownAction',
   'CGI::Session::CookieStore::TamperedWithCookie' ]
end

#handle(args) ⇒ Object

Handle the exception Accepts either an exception, a hash, or an object that responds to to_s

  • Exceptions are passed through like normal

  • Hashes can have an :exception => exception in them, which will result in

    the same treatment as a literal exception passed
    
  • Objects are simply treated as a 'summary' message were an exception may not be necessary


12
13
14
15
16
17
18
19
# File 'lib/chatterbox/exception_notification.rb', line 12

def handle(args)
  hsh = normalize_to_hash(args)
  return if use_ignore_list?(hsh) && on_ignore_list?(hsh[:exception])
  hsh = Extracter.wrap(hsh)
  hsh = RailsExtracter.wrap(hsh)
  hsh = Presenter.render(hsh)
  Chatterbox.notify(hsh)
end

#log_ignored_exception(exception) ⇒ Object


44
45
46
# File 'lib/chatterbox/exception_notification.rb', line 44

def log_ignored_exception(exception)
  Chatterbox.logger.debug { %[Chatterbox::ExceptionNotification ignoring exception: "#{exception.inspect}" as its on the ignore list] }
end

#normalize_to_hash(args) ⇒ Object


21
22
23
24
25
26
27
# File 'lib/chatterbox/exception_notification.rb', line 21

def normalize_to_hash(args)
  case
  when Exception === args then           { :exception => args }
  when args.respond_to?(:to_hash) then   args.to_hash
  when args.respond_to?(:to_s) then      { :summary => args.to_s }
  end
end

#on_ignore_list?(exception) ⇒ Boolean

Returns:

  • (Boolean)

37
38
39
40
41
42
# File 'lib/chatterbox/exception_notification.rb', line 37

def on_ignore_list?(exception)
  ignored = configuration.ignore.include?(exception.class) || 
            configuration.ignore.include?(exception.class.to_s)
  log_ignored_exception(exception) if ignored
  ignored
end

#use_ignore_list?(hsh) ⇒ Boolean

Check to see if we should use ignore list for this exception - first use the value from the passed in options, otherwise use the configuration value (which defaults to true)

Returns:

  • (Boolean)

32
33
34
35
# File 'lib/chatterbox/exception_notification.rb', line 32

def use_ignore_list?(hsh)
  return hsh[:use_ignore_list] if hsh.key?(:use_ignore_list)
  configuration.use_ignore_list
end