rails_exception_handler 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +11 -3
- data/Gemfile.lock +32 -30
- data/README.markdown +2 -2
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/rails_exception_handler.rb +1 -0
- data/lib/rails_exception_handler/handler.rb +7 -1
- data/rails_exception_handler.gemspec +18 -42
- data/spec/testapp_32/.gitignore +15 -0
- data/spec/testapp_32/app/mailers/.gitkeep +0 -0
- data/spec/testapp_32/app/models/.gitkeep +0 -0
- data/spec/testapp_32/lib/assets/.gitkeep +0 -0
- data/spec/testapp_32/lib/tasks/.gitkeep +0 -0
- data/spec/testapp_32/log/.gitkeep +0 -0
- data/spec/testapp_32/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/testapp_32/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/testapp_32/vendor/plugins/.gitkeep +0 -0
- metadata +21 -45
- data/spec/integration/configuration_spec.rb +0 -220
- data/spec/integration/rails_exception_handler_spec.rb +0 -67
- data/spec/spec_helper.rb +0 -24
- data/spec/test_macros.rb +0 -63
- data/spec/testapp_30/Gemfile +0 -31
- data/spec/testapp_30/Gemfile.lock +0 -75
- data/spec/testapp_30/Rakefile +0 -7
- data/spec/testapp_30/app/controllers/application_controller.rb +0 -16
- data/spec/testapp_30/app/controllers/home_controller.rb +0 -22
- data/spec/testapp_30/app/helpers/application_helper.rb +0 -2
- data/spec/testapp_30/app/models/stored_exception.rb +0 -9
- data/spec/testapp_30/app/views/home/view_error.html.erb +0 -2
- data/spec/testapp_30/app/views/layouts/fallback.html.erb +0 -15
- data/spec/testapp_30/app/views/layouts/home.html.erb +0 -15
- data/spec/testapp_30/config.ru +0 -4
- data/spec/testapp_30/config/application.rb +0 -44
- data/spec/testapp_30/config/boot.rb +0 -6
- data/spec/testapp_30/config/environment.rb +0 -5
- data/spec/testapp_30/config/environments/development.rb +0 -26
- data/spec/testapp_30/config/environments/production.rb +0 -49
- data/spec/testapp_30/config/environments/test.rb +0 -35
- data/spec/testapp_30/config/examples/database.yml +0 -13
- data/spec/testapp_30/config/locales/en.yml +0 -5
- data/spec/testapp_30/config/routes.rb +0 -6
- data/spec/testapp_30/db/migrate/20110630174538_create_error_messages.rb +0 -22
- data/spec/testapp_30/db/migrate/20110702131654_add_sessions_table.rb +0 -16
- data/spec/testapp_30/db/schema.rb +0 -39
- data/spec/testapp_30/db/seeds.rb +0 -7
- data/spec/testapp_30/script/rails +0 -6
- data/spec/testapp_30/script/setup +0 -19
- data/spec/unit/configuration_spec.rb +0 -37
- data/spec/unit/handler_spec.rb +0 -200
- data/spec/unit/parser_spec.rb +0 -136
data/spec/unit/handler_spec.rb
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe RailsExceptionHandler::Handler do
|
|
4
|
-
before(:each) do
|
|
5
|
-
@handler = RailsExceptionHandler::Handler.new(create_env, create_exception)
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
describe ".handle_exception" do
|
|
9
|
-
it "should parse error" do
|
|
10
|
-
@handler.handle_exception
|
|
11
|
-
@handler.instance_variable_get(:@parsed_error).should_not == nil
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "should store error" do
|
|
15
|
-
@handler.should_receive(:store_error)
|
|
16
|
-
@handler.handle_exception
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it "should return a rack tripple" do
|
|
20
|
-
response = @handler.handle_exception
|
|
21
|
-
response.length.should == 3
|
|
22
|
-
response[0].should == 500 # response code
|
|
23
|
-
response[1].class.should == Hash # headers
|
|
24
|
-
response[2].class.should == ActionDispatch::Response # body
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it "should set the response code to 404 on routing errors" do
|
|
28
|
-
exception = create_exception
|
|
29
|
-
exception.stub!(:class => ActionController::RoutingError)
|
|
30
|
-
handler = RailsExceptionHandler::Handler.new(create_env, exception)
|
|
31
|
-
response = handler.handle_exception
|
|
32
|
-
response.length.should == 3
|
|
33
|
-
response[0].should == 404
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
describe ".store_error" do
|
|
38
|
-
it "should store an error message in the database when storage_strategies includes :active_record" do
|
|
39
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [:active_record] }
|
|
40
|
-
@handler.handle_exception
|
|
41
|
-
ErrorMessage.count.should == 1
|
|
42
|
-
msg = ErrorMessage.first
|
|
43
|
-
msg.app_name.should == 'ExceptionHandlerTestApp'
|
|
44
|
-
msg.class_name.should == 'NoMethodError'
|
|
45
|
-
msg.message.should == "undefined method `foo' for nil:NilClass"
|
|
46
|
-
msg.trace.should match /active_support\/whiny_nil/
|
|
47
|
-
msg.params.should match /\"foo\"=>\"bar\"/
|
|
48
|
-
msg.user_agent.should == 'Mozilla/4.0 (compatible; MSIE 8.0)'
|
|
49
|
-
msg.target_url.should == 'http://example.org/home?foo=bar'
|
|
50
|
-
msg.referer_url.should == 'http://google.com/'
|
|
51
|
-
msg.created_at.should be > 5.seconds.ago
|
|
52
|
-
msg.created_at.should be < Time.now
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
it "should not store an error message in the database when storage_strategies does not include :active_record" do
|
|
56
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [] }
|
|
57
|
-
ErrorMessage.count.should == 0
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
it "it should log an error to the rails log when storage_strategies includes :rails_log" do
|
|
61
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [:rails_log] }
|
|
62
|
-
read_test_log.should == ''
|
|
63
|
-
@handler.handle_exception
|
|
64
|
-
read_test_log.should match /NoMethodError \(undefined method `foo' for nil:NilClass\)/
|
|
65
|
-
read_test_log.should match /lib\/active_support\/whiny_nil\.rb:48/
|
|
66
|
-
read_test_log.should match /PARAMS:\s+\{\"foo\"=>\"bar\"\}/
|
|
67
|
-
read_test_log.should match /USER_AGENT:\s+Mozilla\/4.0 \(compatible; MSIE 8\.0\)/
|
|
68
|
-
read_test_log.should match /TARGET:\s+http:\/\/example\.org\/home\?foo=bar/
|
|
69
|
-
read_test_log.should match /REFERER:\s+http:\/\/google\.com\//
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it "should not log an error to the rails log when storage_strategies does not include :rails_log" do
|
|
73
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [] }
|
|
74
|
-
read_test_log.should == ''
|
|
75
|
-
@handler.handle_exception
|
|
76
|
-
read_test_log.should == ''
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it "should send the error_message as an HTTP POST request when :remote_url is included" do
|
|
80
|
-
Time.stub!(:now => Time.now) # Otherwise the timestamps will be different, and comparison fail
|
|
81
|
-
@handler.handle_exception
|
|
82
|
-
parser = @handler.instance_variable_get(:@parsed_error)
|
|
83
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [:remote_url => {:target => 'http://example.com/error_messages'}] }
|
|
84
|
-
uri = URI.parse('http://example.com/error_messages')
|
|
85
|
-
params = {}
|
|
86
|
-
parser.relevant_info.each do |key,value|
|
|
87
|
-
params["error_message[#{key}]"] = value
|
|
88
|
-
end
|
|
89
|
-
Net::HTTP.should_receive(:post_form).with(uri, params)
|
|
90
|
-
@handler.handle_exception
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
it "should not send the error_message as an HTTP POST request when :remote_url is not included" do
|
|
94
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [] }
|
|
95
|
-
Net::HTTP.should_not_receive(:post_form)
|
|
96
|
-
@handler.handle_exception
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
it "should be able to use multiple storage strategies" do
|
|
100
|
-
RailsExceptionHandler.configure { |config| config.storage_strategies = [:active_record, :rails_log] }
|
|
101
|
-
read_test_log.should == ''
|
|
102
|
-
@handler.handle_exception
|
|
103
|
-
read_test_log.should match /NoMethodError \(undefined method `foo' for nil:NilClass\)/
|
|
104
|
-
ErrorMessage.count.should == 1
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
describe '.response' do
|
|
109
|
-
it "should call index action on ErrorResponseController" do
|
|
110
|
-
ErrorResponseController.should_receive(:action).with(:index).and_return(mock(Object, :call => [500, {}, {}]))
|
|
111
|
-
@handler.handle_exception
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
it "should set response_code to '404' on routing errors" do
|
|
115
|
-
exception = create_exception
|
|
116
|
-
env = create_env
|
|
117
|
-
exception.stub!(:class => ActiveRecord::RecordNotFound)
|
|
118
|
-
handler = RailsExceptionHandler::Handler.new(env, exception)
|
|
119
|
-
response = handler.handle_exception
|
|
120
|
-
response[0].should == 404
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
it "should set response_code to '500' on all other errors" do
|
|
124
|
-
env = create_env
|
|
125
|
-
handler = RailsExceptionHandler::Handler.new(env, create_exception)
|
|
126
|
-
response = handler.handle_exception
|
|
127
|
-
response[0].should == 500
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
it "should save the layout in env" do
|
|
131
|
-
env = create_env
|
|
132
|
-
handler = RailsExceptionHandler::Handler.new(env, create_exception)
|
|
133
|
-
handler.instance_variable_set("@controller",mock(Object, :_default_layout => 'home'))
|
|
134
|
-
handler.handle_exception
|
|
135
|
-
env['exception_handler.layout'].should == 'home'
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
it "should use the fallback layout when no layout is defined" do
|
|
139
|
-
env = create_env
|
|
140
|
-
handler = RailsExceptionHandler::Handler.new(env, create_exception)
|
|
141
|
-
handler.handle_exception
|
|
142
|
-
env['exception_handler.layout'].should == 'fallback'
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
describe '.response_layout' do
|
|
147
|
-
it "should not set a layout for XHR requests" do
|
|
148
|
-
handler = RailsExceptionHandler::Handler.new(create_env, create_exception)
|
|
149
|
-
handler.handle_exception
|
|
150
|
-
request = handler.instance_variable_get(:@request)
|
|
151
|
-
request.stub!(:xhr? => true)
|
|
152
|
-
handler.instance_variable_set(:@request, request)
|
|
153
|
-
handler.send(:response_layout).should == false
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
it "should use the controllers default layout if it exists" do
|
|
157
|
-
env = create_env
|
|
158
|
-
controller = ApplicationController.new
|
|
159
|
-
controller.stub!(:_default_layout => 'home')
|
|
160
|
-
env['action_controller.instance'] = controller
|
|
161
|
-
handler = RailsExceptionHandler::Handler.new(env, create_exception)
|
|
162
|
-
handler.handle_exception
|
|
163
|
-
handler.send(:response_layout).should == 'home'
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
it "should use the fallback layout if the controller does not have a default layout" do
|
|
167
|
-
handler = RailsExceptionHandler::Handler.new(create_env, create_exception)
|
|
168
|
-
handler.handle_exception
|
|
169
|
-
handler.send(:response_layout).should == 'fallback'
|
|
170
|
-
end
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
describe '.response_text' do
|
|
174
|
-
it "should return the response mapped to the exception class if it exists" do
|
|
175
|
-
RailsExceptionHandler.configure { |config|
|
|
176
|
-
config.responses[:not_found] = 'Page not found'
|
|
177
|
-
config.response_mapping = {'ActiveRecord::RecordNotFound' => :not_found}
|
|
178
|
-
}
|
|
179
|
-
env = create_env
|
|
180
|
-
exception = create_exception
|
|
181
|
-
exception.stub!(:class => ActiveRecord::RecordNotFound)
|
|
182
|
-
handler = RailsExceptionHandler::Handler.new(env, exception)
|
|
183
|
-
handler.handle_exception
|
|
184
|
-
handler.send(:response_text).should == 'Page not found'
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
it "should return the default response if a mapping does not exist" do
|
|
188
|
-
RailsExceptionHandler.configure { |config|
|
|
189
|
-
config.responses[:default] = 'Default response'
|
|
190
|
-
config.response_mapping = {}
|
|
191
|
-
}
|
|
192
|
-
env = create_env
|
|
193
|
-
exception = create_exception
|
|
194
|
-
exception.stub!(:class => ActiveRecord::RecordNotFound)
|
|
195
|
-
handler = RailsExceptionHandler::Handler.new(env, exception)
|
|
196
|
-
handler.handle_exception
|
|
197
|
-
handler.send(:response_text).should == 'Default response'
|
|
198
|
-
end
|
|
199
|
-
end
|
|
200
|
-
end
|
data/spec/unit/parser_spec.rb
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe RailsExceptionHandler::Parser do
|
|
4
|
-
before(:each) do
|
|
5
|
-
env = create_env
|
|
6
|
-
controller = mock(ApplicationController, :current_user => mock(Object, :login => 'matz'))
|
|
7
|
-
request = ActionDispatch::Request.new(env)
|
|
8
|
-
@parser = RailsExceptionHandler::Parser.new(create_exception, request, controller)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
describe ".relevant_info" do
|
|
12
|
-
it("should return app_name") { @parser.relevant_info[:app_name].should == 'ExceptionHandlerTestApp' }
|
|
13
|
-
it("should return class_name") { @parser.relevant_info[:class_name].should == 'NoMethodError' }
|
|
14
|
-
it("should return message") { @parser.relevant_info[:message].should == "undefined method `foo' for nil:NilClass" }
|
|
15
|
-
it("should return trace") { @parser.relevant_info[:trace].should match /active_support\/whiny_nil/ }
|
|
16
|
-
it("should return target_url") { @parser.relevant_info[:target_url].should == 'http://example.org/home?foo=bar' }
|
|
17
|
-
it("should return referer_url") { @parser.relevant_info[:referer_url].should == 'http://google.com/' }
|
|
18
|
-
it("should return params") { @parser.relevant_info[:params].should match(/\"foo\"=>\"bar\"/) }
|
|
19
|
-
it("should return user_agent") { @parser.relevant_info[:user_agent].should == "Mozilla/4.0 (compatible; MSIE 8.0)" }
|
|
20
|
-
it("should return user_info") { @parser.relevant_info[:user_info].should == nil }
|
|
21
|
-
it("should return created_at") { @parser.relevant_info[:created_at].should be > 5.seconds.ago }
|
|
22
|
-
it("should return created_at") { @parser.relevant_info[:created_at].should be < Time.now }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
describe ".ignore?" do
|
|
26
|
-
context "routing errors" do
|
|
27
|
-
it "should return true on routing errors when the filter contains :all_404s" do
|
|
28
|
-
RailsExceptionHandler.configure { |config| config.filters = [:all_404s] }
|
|
29
|
-
exception = create_exception
|
|
30
|
-
exception.stub!(:class => ActionController::RoutingError)
|
|
31
|
-
parser = create_parser(exception, nil, nil)
|
|
32
|
-
parser.ignore?.should == true
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it "should return true on routing errors without referer when the filter contains :no_referer_404s" do
|
|
36
|
-
RailsExceptionHandler.configure { |config| config.filters = [:no_referer_404s] }
|
|
37
|
-
exception = create_exception
|
|
38
|
-
exception.stub!(:class => ActionController::RoutingError)
|
|
39
|
-
request = ActionDispatch::Request.new(create_env(:referer => '/'))
|
|
40
|
-
parser = create_parser(exception, request, nil)
|
|
41
|
-
parser.ignore?.should == true
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it "should return true when the user agent matches against the filters :user_agent_regxp" do
|
|
45
|
-
RailsExceptionHandler.configure { |config| config.filters = [{:user_agent_regxp => /\b(Mozilla)\b/}] }
|
|
46
|
-
parser = create_parser(nil, nil, nil)
|
|
47
|
-
parser.ignore?.should == true
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it "should return true when the url matches against the filters :target_url_regxp" do
|
|
51
|
-
RailsExceptionHandler.configure { |config| config.filters = [{:target_url_regxp => /\b(home)\b/}] }
|
|
52
|
-
parser = create_parser(nil, nil, nil)
|
|
53
|
-
parser.ignore?.should == true
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it "should return false when the request is not caught by a filter" do
|
|
57
|
-
RailsExceptionHandler.configure { |config| config.filters = [] }
|
|
58
|
-
parser = create_parser(nil, nil, nil)
|
|
59
|
-
parser.ignore?.should == false
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
describe "routing_error?" do
|
|
65
|
-
it "should return true on ActionController::RoutingError" do
|
|
66
|
-
exception = create_exception
|
|
67
|
-
exception.stub!(:class => ActionController::RoutingError)
|
|
68
|
-
parser = create_parser(exception, nil, nil)
|
|
69
|
-
parser.routing_error?.should == true
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it "should return true on AbstractController::ActionNotFound" do
|
|
73
|
-
exception = create_exception
|
|
74
|
-
exception.stub!(:class => AbstractController::ActionNotFound)
|
|
75
|
-
parser = create_parser(exception, nil, nil)
|
|
76
|
-
parser.routing_error?.should == true
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it "should return true on ActiveRecord::RecordNotFound" do
|
|
80
|
-
exception = create_exception
|
|
81
|
-
exception.stub!(:class => ActiveRecord::RecordNotFound)
|
|
82
|
-
parser = create_parser(exception, nil, nil)
|
|
83
|
-
parser.routing_error?.should == true
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
it "should return false on all other errors" do
|
|
87
|
-
@parser.routing_error?.should == false
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
describe "user_info" do
|
|
92
|
-
it "should store user info based on the method and field provided" do
|
|
93
|
-
RailsExceptionHandler.configure {|config| config.store_user_info = {:method => :current_user, :field => :login}}
|
|
94
|
-
controller = mock(ApplicationController, :current_user => mock(Object, :login => 'matz'))
|
|
95
|
-
parser = create_parser(nil, nil, controller)
|
|
96
|
-
parser.relevant_info[:user_info].should == 'matz'
|
|
97
|
-
end
|
|
98
|
-
it "should store 'Anonymous' when store_user_info is enabled and no user is logged in" do
|
|
99
|
-
RailsExceptionHandler.configure {|config| config.store_user_info = {:method => :current_user, :field => :login}}
|
|
100
|
-
controller = mock(ApplicationController, :current_user => nil)
|
|
101
|
-
parser = create_parser(nil, nil, controller)
|
|
102
|
-
parser.relevant_info[:user_info].should == 'Anonymous'
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
it "should not store any info when configured store_user_info is false" do
|
|
106
|
-
RailsExceptionHandler.configure {|config| config.store_user_info = false}
|
|
107
|
-
controller = mock(ApplicationController, :current_user => mock(Object, :login => 'matz'))
|
|
108
|
-
parser = create_parser(nil, nil, controller)
|
|
109
|
-
parser.relevant_info[:user_info].should == nil
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
describe "anon_user?" do
|
|
114
|
-
it "should return true if user_info is nil" do
|
|
115
|
-
RailsExceptionHandler.configure {|config| config.store_user_info = false}
|
|
116
|
-
controller = mock(ApplicationController)
|
|
117
|
-
parser = create_parser(nil, nil, controller)
|
|
118
|
-
parser.anon_user?.should be_true
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
it "should return true if user_info is 'Anonymous'" do
|
|
122
|
-
RailsExceptionHandler.configure {|config| config.store_user_info = {:method => :current_user, :field => :login}}
|
|
123
|
-
controller = mock(ApplicationController, :current_user => nil)
|
|
124
|
-
parser = create_parser(nil, nil, controller)
|
|
125
|
-
parser.relevant_info[:user_info].should == 'Anonymous'
|
|
126
|
-
parser.anon_user?.should be_true
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
it "should return false if user info is present" do
|
|
130
|
-
RailsExceptionHandler.configure {|config| config.store_user_info = {:method => :current_user, :field => :login}}
|
|
131
|
-
controller = mock(ApplicationController, :current_user => mock(Object, :login => 'matz'))
|
|
132
|
-
parser = create_parser(nil, nil, controller)
|
|
133
|
-
parser.anon_user?.should be_false
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
end
|