rails_exception_handler 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,6 +1,10 @@
1
1
  CHANGELOG
2
2
 
3
3
 
4
+ v1.1.0 - 18. Jul 2011
5
+ - Better handling of ajax requests
6
+ - Exceptions can be mapped to specific responses
7
+
4
8
  v1.0.1 - 17. Jul 2011
5
9
  - Minor fix for 1.8.7 compatibility
6
10
 
data/README.markdown CHANGED
@@ -23,14 +23,21 @@ RailsExceptionHandler.configure do |config|
23
23
  # config.storage_strategies = [:active_record, :rails_log, :remote_url => {:target => 'http://example.com'}] # Defaults to []
24
24
  # config.fallback_layout = 'home' # Defaults to 'application'
25
25
  # config.store_user_info = {:method => :current_user, :field => :login} # Defaults to false
26
- # config.responses['404'] = "<h1>404</h1><p>Page not found</p>"
27
- # config.responses['500'] = "<h1>500</h1><p>Internal server error</p>"
28
26
  # config.filters = [ # No filters are enabled by default
29
27
  # :all_404s,
30
28
  # :no_referer_404s,
31
29
  # {:user_agent_regxp => /\b(ApptusBot|TurnitinBot|DotBot|SiteBot)\b/i},
32
30
  # {:target_url_regxp => /\b(myphpadmin)\b/i}
33
- #]
31
+ # ]
32
+ # config.responses = { # There must be a default response. The rest is up to you.
33
+ # :default => "<h1>500</h1><p>Internal server error</p>"
34
+ # :custom => "<h1>404</h1><p>Page not found</p>",
35
+ # }
36
+ # config.response_mapping = { # All errors are mapped to the :default response unless overridden here
37
+ # 'ActiveRecord::RecordNotFound' => :not_found,
38
+ # 'ActionController:RoutingError' => :not_found,
39
+ # 'AbstractController::ActionNotFound' => :not_found,
40
+ # }
34
41
  end
35
42
  ```
36
43
 
@@ -77,6 +84,24 @@ config.store_user_info = {:method => :current_user, :field => :login}
77
84
  Default value: false (no info will be stored)
78
85
  If you turn this on and the error is generated by a client that is not logged in, then "Anonymous" will be used.
79
86
 
87
+ ### responses and response_mapping
88
+
89
+ Create a set of responses and then map specific exceptions to these responses. There needs to be a response called :default which is used for the exceptions that are not explicity mapped to a response.
90
+
91
+ ```ruby
92
+ config.responses = {
93
+ :default => "<h1>500</h1><p>Internal server error</p>",
94
+ :not_found => "<h1>404</h1><p>Page not found</p>",
95
+ :wrong_token => "<h1>500</h1>There was a problem authenticating the submitted form. Reload the page and try again."
96
+ }
97
+ config.response_mapping = {
98
+ 'ActiveRecord::RecordNotFound' => :not_found,
99
+ 'ActionController:RoutingError' => :not_found,
100
+ 'AbstractController::ActionNotFound' => :not_found,
101
+ 'ActionController::InvalidAuthenticityToken' => :wrong_token
102
+ }
103
+ ```
104
+
80
105
  ### filters
81
106
 
82
107
  All filters are disabled by default. I recommend you deploy your application this way, and then add filters as they become necessary.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -1,6 +1,5 @@
1
1
  class ErrorResponseController < ApplicationController
2
2
  def index
3
- render(:text => RailsExceptionHandler.configuration.responses[@_env['exception_handler.response_code']],
4
- :layout => @_env['exception_handler.layout'])
3
+ render(:text => @_env['exception_handler.response'], :layout => @_env['exception_handler.layout'])
5
4
  end
6
5
  end
@@ -1,5 +1,5 @@
1
1
  class RailsExceptionHandler::Configuration
2
- attr_accessor :storage_strategies, :environments, :filters, :responses, :fallback_layout, :store_user_info
2
+ attr_accessor :storage_strategies, :environments, :filters, :responses, :response_mapping, :fallback_layout, :store_user_info
3
3
 
4
4
  def initialize
5
5
  @environments = [:production]
@@ -7,7 +7,7 @@ class RailsExceptionHandler::Configuration
7
7
  @filters = []
8
8
  @store_user_info = false
9
9
  @fallback_layout = 'application'
10
- @responses = { '404' => '<h1>Page not found</h1><p>The page you were looking for could not be found.</p>',
11
- '500' => '<h1>Internal server error</h1><p>The application has encountered an unexpected issue.</p>' }
10
+ @response_mapping = {}
11
+ @responses = { :default => '<h1>Internal server error</h1><p>The application has encountered an unexpected issue.</p>' }
12
12
  end
13
13
  end
@@ -62,8 +62,25 @@ class RailsExceptionHandler::Handler
62
62
  end
63
63
 
64
64
  def response
65
- @env['exception_handler.layout'] = @controller.send(:_default_layout) || RailsExceptionHandler.configuration.fallback_layout
66
- @env['exception_handler.response_code'] = @parsed_error.routing_error? ? '404' : '500'
67
- ErrorResponseController.action(:index).call(@env)
65
+ @env['exception_handler.layout'] = response_layout
66
+ @env['exception_handler.response'] = response_text
67
+ response = ErrorResponseController.action(:index).call(@env)
68
+ response[0] = @parsed_error.routing_error? ? 404 : 500
69
+ return response
70
+ end
71
+
72
+ def response_layout
73
+ if(@request.xhr?)
74
+ false
75
+ else
76
+ @controller.send(:_default_layout) || RailsExceptionHandler.configuration.fallback_layout
77
+ end
78
+ end
79
+
80
+ def response_text
81
+ config = RailsExceptionHandler.configuration
82
+ klass = @parsed_error.relevant_info[:class_name]
83
+ key = config.response_mapping[klass] || :default
84
+ return config.responses[key]
68
85
  end
69
86
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_exception_handler}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sharagoz"]
12
- s.date = %q{2011-07-17}
12
+ s.date = %q{2011-07-18}
13
13
  s.description = %q{}
14
14
  s.email = %q{contact@sharagoz.com}
15
15
  s.extra_rdoc_files = [
@@ -158,4 +158,27 @@ describe RailsExceptionHandler::Configuration do
158
158
  ErrorMessage.first.user_info.should == 'matz'
159
159
  end
160
160
  end
161
+
162
+ describe "reponses and response_mapping" do
163
+ it "should use the default response on non-mapped errors" do
164
+ RailsExceptionHandler.configure do |config|
165
+ config.environments = [Rails.env.to_sym]
166
+ config.storage_strategies = []
167
+ config.responses = {:default => 'Customized response'}
168
+ end
169
+ get('/home/view_error')
170
+ last_response.body.should match(/Customized response/)
171
+ end
172
+
173
+ it "should use mapped response where they exist" do
174
+ RailsExceptionHandler.configure do |config|
175
+ config.environments = [Rails.env.to_sym]
176
+ config.storage_strategies = []
177
+ config.responses[:not_found] = 'custom_routing_response_text'
178
+ config.response_mapping['ActionController::RoutingError'] = :not_found
179
+ end
180
+ get('/incorrect_route')
181
+ last_response.body.should match(/custom_routing_response_text/)
182
+ end
183
+ end
161
184
  end
@@ -25,7 +25,7 @@ describe RailsExceptionHandler do
25
25
  it "should catch routing errors" do
26
26
  get "/incorrect_route"
27
27
  ErrorMessage.count.should == 1
28
- last_response.body.should match(/Page not found/)
28
+ last_response.body.should match(/Internal server error/)
29
29
  ErrorMessage.first.class_name.should == 'ActionController::RoutingError'
30
30
  end
31
31
 
data/spec/test_macros.rb CHANGED
@@ -50,6 +50,8 @@ module TestMacros
50
50
  config.store_user_info = false
51
51
  config.filters = []
52
52
  config.fallback_layout = 'fallback'
53
+ config.response_mapping = {}
54
+ config.responses = { :default => '<h1>Internal server error</h1><p>The application has encountered an unexpected issue.</p>' }
53
55
  end
54
56
  end
55
57
 
@@ -22,12 +22,12 @@ describe RailsExceptionHandler::Configuration do
22
22
  @configuration.filters.should == []
23
23
  end
24
24
 
25
- it "should set the 404 response" do
26
- @configuration.responses['404'].should match(/Page not found/)
25
+ it "should set the default response" do
26
+ @configuration.responses.should == {:default => '<h1>Internal server error</h1><p>The application has encountered an unexpected issue.</p>' }
27
27
  end
28
28
 
29
- it "should set the 500 response" do
30
- @configuration.responses['500'].should match(/Internal server error/)
29
+ it "should set the reponse_mapping to {}" do
30
+ @configuration.response_mapping.should == {}
31
31
  end
32
32
 
33
33
  it "should set the fallback layout to 'application'" do
@@ -19,10 +19,19 @@ describe RailsExceptionHandler::Handler do
19
19
  it "should return a rack tripple" do
20
20
  response = @handler.handle_exception
21
21
  response.length.should == 3
22
- response[0].should == 200 # response code
22
+ response[0].should == 500 # response code
23
23
  response[1].class.should == Hash # headers
24
24
  response[2].class.should == ActionDispatch::Response # body
25
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
26
35
  end
27
36
 
28
37
  describe ".store_error" do
@@ -98,7 +107,7 @@ describe RailsExceptionHandler::Handler do
98
107
 
99
108
  describe '.response' do
100
109
  it "should call index action on ErrorResponseController" do
101
- ErrorResponseController.should_receive(:action).with(:index).and_return(mock(Object, :call => true))
110
+ ErrorResponseController.should_receive(:action).with(:index).and_return(mock(Object, :call => [500, {}, {}]))
102
111
  @handler.handle_exception
103
112
  end
104
113
 
@@ -107,15 +116,15 @@ describe RailsExceptionHandler::Handler do
107
116
  env = create_env
108
117
  exception.stub!(:class => ActiveRecord::RecordNotFound)
109
118
  handler = RailsExceptionHandler::Handler.new(env, exception)
110
- handler.handle_exception
111
- env['exception_handler.response_code'].should == '404'
119
+ response = handler.handle_exception
120
+ response[0].should == 404
112
121
  end
113
122
 
114
123
  it "should set response_code to '500' on all other errors" do
115
124
  env = create_env
116
125
  handler = RailsExceptionHandler::Handler.new(env, create_exception)
117
- handler.handle_exception
118
- env['exception_handler.response_code'].should == '500'
126
+ response = handler.handle_exception
127
+ response[0].should == 500
119
128
  end
120
129
 
121
130
  it "should save the layout in env" do
@@ -133,4 +142,59 @@ describe RailsExceptionHandler::Handler do
133
142
  env['exception_handler.layout'].should == 'fallback'
134
143
  end
135
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
136
200
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rails_exception_handler
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sharagoz
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-17 00:00:00 +02:00
13
+ date: 2011-07-18 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency