This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
/
exception_notification.rb
81 lines (69 loc) · 2.86 KB
/
exception_notification.rb
1
2
3
4
5
6
7
8
9
10
11
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'ostruct'
module Chatterbox
module ExceptionNotification
# 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
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
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
# 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)
def use_ignore_list?(hsh)
return hsh[:use_ignore_list] if hsh.key?(:use_ignore_list)
configuration.use_ignore_list
end
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
def log_ignored_exception(exception)
Chatterbox.logger.debug { %[Chatterbox::ExceptionNotification ignoring exception: "#{exception}" as its on the ignore list] }
end
# 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
def configuration
@configuration ||= OpenStruct.new(
:ignore => default_ignored_exceptions,
:use_ignore_list => true)
end
# Configure ExceptionNotification
# ex:
# Chatterbox::ExceptionNotification.configure do |config|
# config.ignore << MyOwnExceptionToIgnore
# end
def configure
yield(configuration)
end
# Default exceptions that ExceptionNotification will ignore
def default_ignored_exceptions
['ActiveRecord::RecordNotFound', 'ActionController::RoutingError',
'ActionController::InvalidAuthenticityToken', 'ActionController::UnknownAction',
'CGI::Session::CookieStore::TamperedWithCookie' ]
end
extend self
end
end
require 'chatterbox/exception_notification/extracter'
require 'chatterbox/exception_notification/rails_extracter'
require 'chatterbox/exception_notification/presenter'