From 08689a2d1f0165f353f62ef4626e87fe841ec735 Mon Sep 17 00:00:00 2001 From: Tijmen Brommet Date: Tue, 3 Nov 2015 21:26:59 +0000 Subject: Add text template for source code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a request is made with AJAX and an error occurs, Rails will render a text-template for the exception instead of the HTML error page (#11960). The `.text.erb` variant of the `_source` template is currently missing, causing HTML to be rendered in the response. This commit adds the text template. To keep the page scannable we only only show the first three source extracts. Related to #14745. Before: ``` ~/testing-exceptions ᐅ curl 'http://localhost:3000/' -H 'X-Requested-With: XMLHttpRequest' RuntimeError in PostsController#index
Extracted source (around line #3):
``` After: ``` ~/testing-exceptions ᐅ curl 'http://localhost:3000/' -H 'X-Requested-With: XMLHttpRequest' RuntimeError in PostsController#index Extracted source (around line #3): *3 raise ``` --- .../middleware/templates/rescues/_source.erb | 27 ---------------------- .../middleware/templates/rescues/_source.html.erb | 27 ++++++++++++++++++++++ .../middleware/templates/rescues/_source.text.erb | 8 +++++++ actionpack/test/dispatch/debug_exceptions_test.rb | 8 +++++++ 4 files changed, 43 insertions(+), 27 deletions(-) delete mode 100644 actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb create mode 100644 actionpack/lib/action_dispatch/middleware/templates/rescues/_source.html.erb create mode 100644 actionpack/lib/action_dispatch/middleware/templates/rescues/_source.text.erb diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb deleted file mode 100644 index e7b913bbe4..0000000000 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb +++ /dev/null @@ -1,27 +0,0 @@ -<% @source_extracts.each_with_index do |source_extract, index| %> - <% if source_extract[:code] %> -
" id="frame-source-<%=index%>"> -
- Extracted source (around line #<%= source_extract[:line_number] %>): -
-
-
- - - - -
-
-                <% source_extract[:code].each_key do |line_number| %>
-<%= line_number -%>
-                <% end %>
-              
-
-
-<% source_extract[:code].each do |line, source| -%>
"><%= source -%>
<% end -%> -
-
-
-
- <% end %> -<% end %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.html.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.html.erb new file mode 100644 index 0000000000..e7b913bbe4 --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.html.erb @@ -0,0 +1,27 @@ +<% @source_extracts.each_with_index do |source_extract, index| %> + <% if source_extract[:code] %> +
" id="frame-source-<%=index%>"> +
+ Extracted source (around line #<%= source_extract[:line_number] %>): +
+
+ + + + + +
+
+                <% source_extract[:code].each_key do |line_number| %>
+<%= line_number -%>
+                <% end %>
+              
+
+
+<% source_extract[:code].each do |line, source| -%>
"><%= source -%>
<% end -%> +
+
+
+
+ <% end %> +<% end %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.text.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.text.erb new file mode 100644 index 0000000000..23a9c7ba3f --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.text.erb @@ -0,0 +1,8 @@ +<% @source_extracts.first(3).each do |source_extract| %> +<% if source_extract[:code] %> +Extracted source (around line #<%= source_extract[:line_number] %>): + +<% source_extract[:code].each do |line, source| -%> +<%= line == source_extract[:line_number] ? "*#{line}" : "##{line}" -%> <%= source -%><% end -%> +<% end %> +<% end %> diff --git a/actionpack/test/dispatch/debug_exceptions_test.rb b/actionpack/test/dispatch/debug_exceptions_test.rb index 89c3e75a50..738fa5fa72 100644 --- a/actionpack/test/dispatch/debug_exceptions_test.rb +++ b/actionpack/test/dispatch/debug_exceptions_test.rb @@ -166,6 +166,14 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest assert_equal "text/plain", response.content_type assert_match(/RuntimeError\npuke/, body) + Rails.stub :root, Pathname.new('.') do + get "/", headers: xhr_request_env + + assert_response 500 + assert_match 'Extracted source (around line #', body + assert_select 'pre', { count: 0 }, body + end + get "/not_found", headers: xhr_request_env assert_response 404 assert_no_match(//, body) -- cgit v1.2.3