public
Fork of thoughtbot/hoptoad_notifier
Description: Reports exceptions to Hoptoad
Homepage: http://www.hoptoadapp.com/
Clone URL: git://github.com/lukeredpath/hoptoad_notifier.git
Having Hoptoad::Notifier and HoptoadNotifier is confusing; the base module can 
just be called Hoptoad. Also found a bug in Hoptoad::Notice where it would fail 
if it was passed nil for request/session
Luke Redpath (author)
Fri Aug 15 07:13:29 -0700 2008
commit  c25d170a51820c65451fdcae64b38b59deaeb46f
tree    8ea02fa5ff903136fa731a7a45396fd70f83a0a1
parent  d0692cfdd60ae2b3135122059a64a9e36b2f1a36
...
 
 
1
2
3
...
19
20
21
22
 
23
24
25
...
1
2
3
4
5
...
21
22
23
 
24
25
26
27
0
@@ -1,3 +1,5 @@
0
+require 'net/http'
0
+
0
 module Hoptoad
0
   class Connection
0
     attr_reader :logger
0
@@ -19,7 +21,7 @@ module Hoptoad
0
         }
0
         http.read_timeout = 5 # seconds
0
         http.open_timeout = 2 # seconds
0
-        # http.use_ssl = HoptoadNotifier.secure
0
+        # http.use_ssl = Hoptoad.secure
0
         response = begin
0
                      http.post(@service_uri.path, stringify_keys(data).to_yaml, headers)
0
                    rescue TimeoutError => e
...
9
10
11
12
 
13
14
15
16
17
 
18
19
20
...
25
26
27
28
 
29
30
31
...
64
65
66
67
 
68
69
70
...
90
91
92
93
 
94
95
96
...
98
99
100
101
 
102
103
104
...
9
10
11
 
12
13
14
15
16
 
17
18
19
20
...
25
26
27
 
28
29
30
31
...
64
65
66
 
67
68
69
70
...
90
91
92
 
93
94
95
96
...
98
99
100
 
101
102
103
104
0
@@ -9,12 +9,12 @@ module Hoptoad
0
     #  :error_message (string)
0
     #  :backtrace (array)
0
     def initialize(params={})
0
-      @params = default_params.merge(params)
0
+      @params = default_params.merge(params.delete_if { |k,v| v.nil? })
0
     end
0
     
0
     def default_params
0
       {
0
-        :api_key       => HoptoadNotifier.api_key,
0
+        :api_key       => Hoptoad.api_key,
0
         :request       => {},
0
         :session       => {},
0
         :environment   => {},
0
@@ -25,7 +25,7 @@ module Hoptoad
0
     
0
     def environment_data
0
       environment_data = @params[:environment]
0
-      if @params[:request].is_a?(ActionController::CgiRequest)
0
+      if @params[:request].respond_to?(:env)
0
         environment_data.merge!(@params[:request].env.to_hash)
0
       end
0
       environment_data
0
@@ -64,7 +64,7 @@ module Hoptoad
0
     
0
     class << self
0
       
0
-      def build(hash_or_exception, request = nil, session = nil, environment = {})
0
+      def build(hash_or_exception, request = {}, session = {}, environment = {})
0
         error_message = "#{hash_or_exception.class.name}: #{hash_or_exception.message}"
0
         new(
0
           :error_class    => hash_or_exception.class.name, 
0
@@ -90,7 +90,7 @@ module Hoptoad
0
         end
0
 
0
         backtrace.to_a.map do |line|
0
-          HoptoadNotifier.backtrace_filters.inject(line) do |line, proc|
0
+          Hoptoad.backtrace_filters.inject(line) do |line, proc|
0
             proc.call(line)
0
           end
0
         end
0
@@ -98,7 +98,7 @@ module Hoptoad
0
 
0
       def filtered_parameters(parameters)
0
         parameters.each do |k, v|
0
-          parameters[k] = "<filtered>" if HoptoadNotifier.params_filters.any? do |filter|
0
+          parameters[k] = "<filtered>" if Hoptoad.params_filters.any? do |filter|
0
             k.to_s.match(/#{filter}/)
0
           end
0
         end
...
 
 
 
1
2
3
...
1
2
3
4
5
6
0
@@ -1,3 +1,6 @@
0
+require 'hoptoad/connection'
0
+require 'hoptoad/notice'
0
+
0
 module Hoptoad
0
   class Notifier
0
     def initialize(connection)
...
1
2
3
4
...
1
 
 
2
0
@@ -1,4 +1,2 @@
1
 require File.join(File.dirname(__FILE__), *%w[.. hoptoad_notifier])
0
-require File.join(File.dirname(__FILE__), *%w[connection])
0
-require File.join(File.dirname(__FILE__), *%w[notice])
0
 require File.join(File.dirname(__FILE__), *%w[notifier])
...
1
2
3
4
5
6
7
8
 
9
 
10
11
12
...
40
41
42
43
 
44
45
46
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 
173
174
...
 
 
 
 
 
 
 
 
1
2
3
4
5
6
...
34
35
36
 
37
38
39
40
...
105
106
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
109
110
111
112
 
113
114
115
0
@@ -1,12 +1,6 @@
0
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'hoptoad'))
0
-require 'net/http'
0
-require 'connection'
0
-require 'notice'
0
-require 'notifier'
0
-
0
-# Plugin for applications to automatically post errors to the Hoptoad of their choice.
0
-module HoptoadNotifier
0
+require 'hoptoad/catcher'
0
 
0
+module Hoptoad
0
   IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
0
                     'ActionController::RoutingError',
0
                     'ActionController::InvalidAuthenticityToken',
0
@@ -40,7 +34,7 @@ module HoptoadNotifier
0
     
0
     # Returns the list of errors that are being ignored. The array can be appended to.
0
     def ignore
0
-      @ignore ||= (HoptoadNotifier::IGNORE_DEFAULT.dup)
0
+      @ignore ||= (Hoptoad::IGNORE_DEFAULT.dup)
0
       @ignore.flatten!
0
       @ignore
0
     end
0
@@ -111,64 +105,11 @@ module HoptoadNotifier
0
     end
0
   end
0
 
0
-  # Include this module in Controllers in which you want to be notified of errors.
0
-  module Catcher
0
-
0
-    def self.included(base) #:nodoc:
0
-      if base.instance_methods.include? 'rescue_action_in_public' and !base.instance_methods.include? 'rescue_action_in_public_without_hoptoad'
0
-        base.alias_method_chain :rescue_action_in_public, :hoptoad
0
-      end
0
-    end
0
-    
0
-    # Overrides the rescue_action method in ActionController::Base, but does not inhibit
0
-    # any custom processing that is defined with Rails 2's exception helpers.
0
-    def rescue_action_in_public_with_hoptoad(exception)
0
-      notify_hoptoad(exception) unless ignore?(exception)
0
-      rescue_action_in_public_without_hoptoad(exception)
0
-    end 
0
-        
0
-    # This method should be used for sending manual notifications while you are still
0
-    # inside the controller. Otherwise it works like HoptoadNotifier.notify. 
0
-    def notify_hoptoad(hash_or_exception)
0
-      if public_environment?
0
-        hoptoad_notifier.notify(hash_or_exception, rails_request, rails_session, ENV.to_hash)
0
-      end
0
-    end
0
-    alias_method :inform_hoptoad, :notify_hoptoad
0
-    
0
-    def hoptoad_notifier
0
-      @hoptoad_notifier ||= Hoptoad::Notifier.new(hoptoad_connection)
0
-    end
0
-    
0
-    def hoptoad_connection
0
-      Hoptoad::Connection.from_config(HoptoadNotifier)
0
-    end
0
-    
0
-    def rails_request
0
-      respond_to?(:request) ? request : nil
0
-    end
0
-    
0
-    def rails_session
0
-      respond_to?(:session) ? session : nil
0
-    end
0
-
0
-    private
0
-    
0
-    def public_environment? #nodoc:
0
-      defined?(RAILS_ENV) and !['development', 'test'].include?(RAILS_ENV)
0
-    end
0
-    
0
-    def ignore?(exception) #:nodoc:
0
-      ignore_these = HoptoadNotifier.ignore.flatten
0
-      ignore_these.include?(exception.class) || ignore_these.include?(exception.class.name)
0
-    end
0
-  end
0
-
0
   # A dummy class for sending notifications manually outside of a controller.
0
   class Sender
0
     def rescue_action_in_public(exception)
0
     end
0
 
0
-    include HoptoadNotifier::Catcher
0
+    include Hoptoad::Catcher
0
   end
0
 end
...
6
7
8
9
10
 
 
11
12
13
...
6
7
8
 
 
9
10
11
12
13
0
@@ -6,8 +6,8 @@ class HoptoadConnectionFunctionalTest < Test::Unit::TestCase
0
   context "Sending data over the connection" do
0
     setup do
0
       @logger = stub_everything('Logger')
0
-      HoptoadNotifier.stubs(:logger).returns(@logger)
0
-      @connection = Hoptoad::Connection.from_config(HoptoadNotifier)
0
+      Hoptoad.stubs(:logger).returns(@logger)
0
+      @connection = Hoptoad::Connection.from_config(Hoptoad)
0
     end
0
     
0
     should "POST the data as YAML to Hoptoad's server" do
...
11
12
13
14
 
15
16
17
...
81
82
83
84
 
85
86
87
...
96
97
98
99
 
100
101
102
...
11
12
13
 
14
15
16
17
...
81
82
83
 
84
85
86
87
...
96
97
98
 
99
100
101
102
0
@@ -11,7 +11,7 @@ end
0
 class HoptoadNoticeFunctionalTest < Test::Unit::TestCase
0
   
0
   def setup
0
-    HoptoadNotifier.stubs(:api_key).returns("000011110000")
0
+    Hoptoad.stubs(:api_key).returns("000011110000")
0
   end
0
   
0
   def test_notice_with_all_available_data_returns_the_correct_payload
0
@@ -81,7 +81,7 @@ class HoptoadNoticeFunctionalTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_notice_should_censor_any_filtered_parameters_that_have_been_configured
0
-    HoptoadNotifier.params_filters << "top_secret_parameter"
0
+    Hoptoad.params_filters << "top_secret_parameter"
0
     
0
     request = ActionController::TestRequest.new({
0
       'action'     => 'my_action',
0
@@ -96,7 +96,7 @@ class HoptoadNoticeFunctionalTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_should_pass_backtrace_through_any_configured_filters
0
-    HoptoadNotifier.filter_backtrace do |line|
0
+    Hoptoad.filter_backtrace do |line|
0
       line.gsub("/my/private/server/folder", "[PRIVATE_FOLDER]")
0
     end
0
     
...
33
34
35
36
 
37
38
39
...
48
49
50
51
 
52
53
54
...
56
57
58
59
 
60
61
62
63
 
64
65
66
...
69
70
71
72
 
73
74
75
...
77
78
79
80
81
82
83
84
 
 
 
 
 
85
86
87
88
89
 
 
90
91
92
93
94
 
 
95
96
97
...
100
101
102
103
104
 
 
105
106
107
108
109
110
111
 
 
112
113
114
115
 
116
117
118
...
143
144
145
146
 
147
148
149
150
151
 
152
153
154
...
218
219
220
221
 
222
223
224
...
248
249
250
251
 
252
253
254
...
281
282
283
284
 
285
286
287
...
290
291
292
293
294
 
 
295
296
297
298
299
300
 
301
302
303
304
305
306
 
307
308
309
310
 
311
312
313
314
315
 
316
317
318
...
33
34
35
 
36
37
38
39
...
48
49
50
 
51
52
53
54
...
56
57
58
 
59
60
61
62
 
63
64
65
66
...
69
70
71
 
72
73
74
75
...
77
78
79
 
 
 
 
 
80
81
82
83
84
85
86
87
 
 
88
89
90
91
92
 
 
93
94
95
96
97
...
100
101
102
 
 
103
104
105
106
107
108
109
 
 
110
111
112
113
114
 
115
116
117
118
...
143
144
145
 
146
147
148
149
150
 
151
152
153
154
...
218
219
220
 
221
222
223
224
...
248
249
250
 
251
252
253
254
...
281
282
283
 
284
285
286
287
...
290
291
292
 
 
293
294
295
296
297
298
299
 
300
301
302
303
304
305
 
306
307
308
309
 
310
311
312
313
314
 
315
316
317
318
0
@@ -33,7 +33,7 @@ class HoptoadController < ActionController::Base
0
   end
0
 end
0
 
0
-class HoptoadNotifierTest < Test::Unit::TestCase
0
+class HoptoadTest < Test::Unit::TestCase
0
   def request(action = nil, method = :get)
0
     @request = ActionController::TestRequest.new({
0
       "controller" => "hoptoad",
0
@@ -48,7 +48,7 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
     should "be able to occur even outside Rails controllers" do    
0
       assert_nothing_raised do
0
         class MyHoptoad
0
-          include HoptoadNotifier::Catcher
0
+          include Hoptoad::Catcher
0
         end
0
       end
0
       my = MyHoptoad.new
0
@@ -56,11 +56,11 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
     end
0
   end
0
   
0
-  context "HoptoadNotifier configuration" do
0
+  context "Hoptoad configuration" do
0
     setup do
0
       @controller = HoptoadController.new
0
       class ::HoptoadController
0
-        include HoptoadNotifier::Catcher
0
+        include Hoptoad::Catcher
0
         def rescue_action e
0
           rescue_action_in_public e
0
         end
0
@@ -69,7 +69,7 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
     end
0
 
0
     should "be done with a block" do
0
-      HoptoadNotifier.configure do |config|
0
+      Hoptoad.configure do |config|
0
         config.host = "host"
0
         config.port = 3333
0
         config.secure = true
0
@@ -77,21 +77,21 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
         config.ignore << [ RuntimeError ]
0
       end
0
       
0
-      assert_equal "host",              HoptoadNotifier.host
0
-      assert_equal 3333,                HoptoadNotifier.port
0
-      assert_equal true,                HoptoadNotifier.secure
0
-      assert_equal "1234567890abcdef",  HoptoadNotifier.api_key
0
-      assert_equal (HoptoadNotifier::IGNORE_DEFAULT + [RuntimeError]), HoptoadNotifier.ignore
0
+      assert_equal "host",              Hoptoad.host
0
+      assert_equal 3333,                Hoptoad.port
0
+      assert_equal true,                Hoptoad.secure
0
+      assert_equal "1234567890abcdef",  Hoptoad.api_key
0
+      assert_equal (Hoptoad::IGNORE_DEFAULT + [RuntimeError]), Hoptoad.ignore
0
     end
0
 
0
     should "set a default host" do
0
-      HoptoadNotifier.instance_variable_set("@host",nil)
0
-      assert_equal "hoptoadapp.com", HoptoadNotifier.host
0
+      Hoptoad.instance_variable_set("@host",nil)
0
+      assert_equal "hoptoadapp.com", Hoptoad.host
0
     end
0
     
0
     should "add filters to the backtrace_filters" do
0
-      assert_difference "HoptoadNotifier.backtrace_filters.length" do
0
-        HoptoadNotifier.configure do |config|
0
+      assert_difference "Hoptoad.backtrace_filters.length" do
0
+        Hoptoad.configure do |config|
0
           config.filter_backtrace do |line|
0
             line = "1234"
0
           end
0
@@ -100,19 +100,19 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
     end
0
     
0
     should "add filters to the params filters" do
0
-      assert_difference "HoptoadNotifier.params_filters.length", 2 do
0
-        HoptoadNotifier.configure do |config|
0
+      assert_difference "Hoptoad.params_filters.length", 2 do
0
+        Hoptoad.configure do |config|
0
           config.params_filters << "abc"
0
           config.params_filters << "def"
0
         end
0
       end
0
 
0
-      assert HoptoadNotifier.params_filters.include?( "abc" )
0
-      assert HoptoadNotifier.params_filters.include?( "def" )
0
+      assert Hoptoad.params_filters.include?( "abc" )
0
+      assert Hoptoad.params_filters.include?( "def" )
0
     end
0
     
0
     should "have at default ignored exceptions" do
0
-      assert HoptoadNotifier::IGNORE_DEFAULT.any?
0
+      assert Hoptoad::IGNORE_DEFAULT.any?
0
     end
0
   end
0
 
0
@@ -143,12 +143,12 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
     context "with the notifier installed" do
0
       setup do
0
         class ::HoptoadController
0
-          include HoptoadNotifier::Catcher
0
+          include Hoptoad::Catcher
0
           def rescue_action e
0
             rescue_action_in_public e
0
           end
0
         end
0
-        HoptoadNotifier.ignore_only = HoptoadNotifier::IGNORE_DEFAULT
0
+        Hoptoad.ignore_only = Hoptoad::IGNORE_DEFAULT
0
         @controller.stubs(:public_environment?).returns(true)
0
         @connection = mock('Connection')
0
         Hoptoad::Connection.stubs(:new).returns(@connection)
0
@@ -218,7 +218,7 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
       
0
       context "and configured to ignore additional exceptions" do
0
         setup do
0
-          HoptoadNotifier.ignore << ActiveRecord::StatementInvalid
0
+          Hoptoad.ignore << ActiveRecord::StatementInvalid
0
         end
0
         
0
         should "still ignore default exceptions" do
0
@@ -248,7 +248,7 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
       
0
       context "and configured to ignore only certain exceptions" do
0
         setup do
0
-          HoptoadNotifier.ignore_only = [ActiveRecord::StatementInvalid]
0
+          Hoptoad.ignore_only = [ActiveRecord::StatementInvalid]
0
         end
0
         
0
         should "no longer ignore default exceptions" do
0
@@ -281,7 +281,7 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
   context "Sending a notice" do
0
     context "with an exception" do
0
       setup do
0
-        @sender    = HoptoadNotifier::Sender.new
0
+        @sender    = Hoptoad::Sender.new
0
         @backtrace = caller
0
         @exception = begin
0
           raise
0
@@ -290,29 +290,29 @@ class HoptoadNotifierTest < Test::Unit::TestCase
0
         end
0
         @options   = {:error_message => "123",
0
                       :backtrace => @backtrace}
0
-        HoptoadNotifier.instance_variable_set("@backtrace_filters", [])
0
-        HoptoadNotifier::Sender.expects(:new).returns(@sender)
0
+        Hoptoad.instance_variable_set("@backtrace_filters", [])
0
+        Hoptoad::Sender.expects(:new).returns(@sender)
0
         @sender.stubs(:public_environment?).returns(true)
0
       end
0
 
0
       should "send as if it were a normally caught exception" do
0
         @sender.expects(:notify_hoptoad).with(@exception)
0
-        HoptoadNotifier.notify(@exception)
0
+        Hoptoad.notify(@exception)
0
       end
0
     end
0
     
0
     context "without an exception" do
0
       setup do
0
-        @sender    = HoptoadNotifier::Sender.new
0
+        @sender    = Hoptoad::Sender.new
0
         @backtrace = caller
0
         @options   = {:error_message => "123",
0
                       :backtrace => @backtrace}
0
-        HoptoadNotifier::Sender.expects(:new).returns(@sender)
0
+        Hoptoad::Sender.expects(:new).returns(@sender)
0
       end
0
 
0
       should "send sensible defaults" do
0
         @sender.expects(:notify_hoptoad).with(@options)
0
-        HoptoadNotifier.notify(:error_message => "123", :backtrace => @backtrace)
0
+        Hoptoad.notify(:error_message => "123", :backtrace => @backtrace)
0
       end
0
     end
0
     

Comments

jnewland Wed Aug 27 06:27:18 -0700 2008 at lib/hoptoad/standalone.rb L1

should be:


require File.join(File.dirname(__FILE__), *%w[.. hoptoad])
lukeredpath Tue Sep 02 02:45:15 -0700 2008

Well spotted!