Skip to content
This repository
Browse code

Display exceptions in text format for xhr request

  • Loading branch information...
commit a725a453b38057b46878afae39dc107ada2cf326 1 parent 998d9c8
Kir Shatrov kirs authored

Showing 15 changed files with 129 additions and 13 deletions. Show diff stats Hide diff stats

  1. +4 0 actionpack/CHANGELOG.md
  2. +21 13 actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
  3. 0  ...ion_dispatch/middleware/templates/rescues/{_request_and_response.erb → _request_and_response.html.erb}
  4. +23 0 actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
  5. 0  actionpack/lib/action_dispatch/middleware/templates/rescues/{_trace.erb → _trace.html.erb}
  6. +15 0 actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
  7. 0  ...ck/lib/action_dispatch/middleware/templates/rescues/{missing_template.erb → missing_template.html.erb}
  8. +3 0  actionpack/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb
  9. 0  actionpack/lib/action_dispatch/middleware/templates/rescues/{routing_error.erb → routing_error.html.erb}
  10. +11 0 actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb
  11. 0  ...onpack/lib/action_dispatch/middleware/templates/rescues/{template_error.erb → template_error.html.erb}
  12. +8 0 actionpack/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb
  13. 0  ...onpack/lib/action_dispatch/middleware/templates/rescues/{unknown_action.erb → unknown_action.html.erb}
  14. +3 0  actionpack/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb
  15. +41 0 actionpack/test/dispatch/debug_exceptions_test.rb
4 actionpack/CHANGELOG.md
Source Rendered
... ... @@ -1,3 +1,7 @@
  1 +* Development mode exceptions are rendered in text format in case of XHR request.
  2 +
  3 + *Kir Shatrov*
  4 +
1 5 * Fix an issue where :if and :unless controller action procs were being run
2 6 before checking for the correct action in the :only and :unless options.
3 7
34 actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
@@ -34,27 +34,35 @@ def render_exception(env, exception)
34 34 log_error(env, wrapper)
35 35
36 36 if env['action_dispatch.show_detailed_exceptions']
  37 + request = Request.new(env)
37 38 template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
38   - :request => Request.new(env),
39   - :exception => wrapper.exception,
40   - :application_trace => wrapper.application_trace,
41   - :framework_trace => wrapper.framework_trace,
42   - :full_trace => wrapper.full_trace,
43   - :routes_inspector => routes_inspector(exception),
44   - :source_extract => wrapper.source_extract,
45   - :line_number => wrapper.line_number,
46   - :file => wrapper.file
  39 + request: request,
  40 + exception: wrapper.exception,
  41 + application_trace: wrapper.application_trace,
  42 + framework_trace: wrapper.framework_trace,
  43 + full_trace: wrapper.full_trace,
  44 + routes_inspector: routes_inspector(exception),
  45 + source_extract: wrapper.source_extract,
  46 + line_number: wrapper.line_number,
  47 + file: wrapper.file
47 48 )
48 49 file = "rescues/#{wrapper.rescue_template}"
49   - body = template.render(:template => file, :layout => 'rescues/layout')
50   - render(wrapper.status_code, body)
  50 +
  51 + if request.xhr?
  52 + body = template.render(template: file, layout: false, formats: [:text])
  53 + format = "text/plain"
  54 + else
  55 + body = template.render(template: file, layout: 'rescues/layout')
  56 + format = "text/html"
  57 + end
  58 + render(wrapper.status_code, body, format)
51 59 else
52 60 raise exception
53 61 end
54 62 end
55 63
56   - def render(status, body)
57   - [status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
  64 + def render(status, body, format)
  65 + [status, {'Content-Type' => "#{format}; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
58 66 end
59 67
60 68 def log_error(env, wrapper)
0  ...eware/templates/rescues/_request_and_response.erb → .../templates/rescues/_request_and_response.html.erb
File renamed without changes
23 actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
... ... @@ -0,0 +1,23 @@
  1 +<%
  2 + clean_params = @request.filtered_parameters.clone
  3 + clean_params.delete("action")
  4 + clean_params.delete("controller")
  5 +
  6 + request_dump = clean_params.empty? ? 'None' : clean_params.inspect.gsub(',', ",\n")
  7 +
  8 + def debug_hash(object)
  9 + object.to_hash.sort_by { |k, _| k.to_s }.map { |k, v| "#{k}: #{v.inspect rescue $!.message}" }.join("\n")
  10 + end unless self.class.method_defined?(:debug_hash)
  11 +%>
  12 +
  13 +Request parameters
  14 +<%= request_dump %>
  15 +
  16 +Session dump
  17 +<%= debug_hash @request.session %>
  18 +
  19 +Env dump
  20 +<%= debug_hash @request.env.slice(*@request.class::ENV_METHODS) %>
  21 +
  22 +Response headers
  23 +<%= defined?(@response) ? @response.headers.inspect.gsub(',', ",\n") : 'None' %>
0  ..._dispatch/middleware/templates/rescues/_trace.erb → ...atch/middleware/templates/rescues/_trace.html.erb
File renamed without changes
15 actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
... ... @@ -0,0 +1,15 @@
  1 +<%
  2 + traces = { "Application Trace" => @application_trace,
  3 + "Framework Trace" => @framework_trace,
  4 + "Full Trace" => @full_trace }
  5 +%>
  6 +
  7 +Rails.root: <%= defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : "unset" %>
  8 +
  9 +<% traces.each do |name, trace| %>
  10 +<% if trace.any? %>
  11 +<%= name %>
  12 +<%= trace.join("\n") %>
  13 +
  14 +<% end %>
  15 +<% end %>
0  ...middleware/templates/rescues/missing_template.erb → ...eware/templates/rescues/missing_template.html.erb
File renamed without changes
3  actionpack/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb
... ... @@ -0,0 +1,3 @@
  1 +Template is missing
  2 +
  3 +<%= @exception.message %>
0  ...ch/middleware/templates/rescues/routing_error.erb → ...ddleware/templates/rescues/routing_error.html.erb
File renamed without changes
11 actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb
... ... @@ -0,0 +1,11 @@
  1 +Routing Error
  2 +
  3 +<%= @exception.message %>
  4 +<% unless @exception.failures.empty? %>
  5 +Failure reasons:
  6 +<% @exception.failures.each do |route, reason| %>
  7 + - <%= route.inspect.delete('\\') %></code> failed because <%= reason.downcase %>
  8 +<% end %>
  9 +<% end %>
  10 +
  11 +<%= render template: "rescues/_trace", format: :text %>
0  ...h/middleware/templates/rescues/template_error.erb → ...dleware/templates/rescues/template_error.html.erb
File renamed without changes
8 actionpack/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb
... ... @@ -0,0 +1,8 @@
  1 +<% @source_extract = @exception.source_extract(0, :html) %>
  2 +<%= @exception.original_exception.class.to_s %> in <%= @request.parameters["controller"].camelize if @request.parameters["controller"] %>#<%= @request.parameters["action"] %>
  3 +
  4 +Showing <%= @exception.file_name %> where line #<%= @exception.line_number %> raised:
  5 +<%= @exception.message %>
  6 +<%= @exception.sub_template_message %>
  7 +<%= render template: "rescues/_trace", format: :text %>
  8 +<%= render template: "rescues/_request_and_response", format: :text %>
0  ...h/middleware/templates/rescues/unknown_action.erb → ...dleware/templates/rescues/unknown_action.html.erb
File renamed without changes
3  actionpack/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb
... ... @@ -0,0 +1,3 @@
  1 +Unknown action
  2 +
  3 +<%= @exception.message %>
41 actionpack/test/dispatch/debug_exceptions_test.rb
@@ -128,6 +128,47 @@ def setup
128 128 assert_match(/ActionController::ParameterMissing/, body)
129 129 end
130 130
  131 + test "rescue with text error for xhr request" do
  132 + @app = DevelopmentApp
  133 + xhr_request_env = {'action_dispatch.show_exceptions' => true, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'}
  134 +
  135 + get "/", {}, xhr_request_env
  136 + assert_response 500
  137 + assert_no_match(/<body>/, body)
  138 + assert_equal response.content_type, "text/plain"
  139 + assert_match(/puke/, body)
  140 +
  141 + get "/not_found", {}, xhr_request_env
  142 + assert_response 404
  143 + assert_no_match(/<body>/, body)
  144 + assert_equal response.content_type, "text/plain"
  145 + assert_match(/#{AbstractController::ActionNotFound.name}/, body)
  146 +
  147 + get "/method_not_allowed", {}, xhr_request_env
  148 + assert_response 405
  149 + assert_no_match(/<body>/, body)
  150 + assert_equal response.content_type, "text/plain"
  151 + assert_match(/ActionController::MethodNotAllowed/, body)
  152 +
  153 + get "/unknown_http_method", {}, xhr_request_env
  154 + assert_response 405
  155 + assert_no_match(/<body>/, body)
  156 + assert_equal response.content_type, "text/plain"
  157 + assert_match(/ActionController::UnknownHttpMethod/, body)
  158 +
  159 + get "/bad_request", {}, xhr_request_env
  160 + assert_response 400
  161 + assert_no_match(/<body>/, body)
  162 + assert_equal response.content_type, "text/plain"
  163 + assert_match(/ActionController::BadRequest/, body)
  164 +
  165 + get "/parameter_missing", {}, xhr_request_env
  166 + assert_response 400
  167 + assert_no_match(/<body>/, body)
  168 + assert_equal response.content_type, "text/plain"
  169 + assert_match(/ActionController::ParameterMissing/, body)
  170 + end
  171 +
131 172 test "does not show filtered parameters" do
132 173 @app = DevelopmentApp
133 174

0 comments on commit a725a45

Please sign in to comment.
Something went wrong with that request. Please try again.