From 5a1b885dd620e6ab465c0f64b7cd0f025a46fb37 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Mon, 31 Dec 2012 14:36:23 -0500 Subject: [PATCH] Add style to AV::Template::Error exception page --- .../middleware/exception_wrapper.rb | 3 +- .../templates/rescues/template_error.erb | 53 ++++++++++++++----- actionpack/lib/action_view/template/error.rb | 16 ++++-- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb index d1cadbd082..ee69c8b49d 100644 --- a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb +++ b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb @@ -57,7 +57,7 @@ module ActionDispatch end def source_extract - if trace = application_trace.first + if application_trace && trace = application_trace.first file, line, _ = trace.split(":") @file = file @line_number = line.to_i @@ -92,6 +92,7 @@ module ActionDispatch end def source_fragment(path, line) + return unless Rails.respond_to?(:root) && Rails.root full_path = Rails.root.join(path) if File.exists?(full_path) File.open(full_path, "r") do |file| diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/template_error.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/template_error.erb index a1b377f68c..01faf5a475 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/template_error.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/template_error.erb @@ -1,17 +1,44 @@ -

- <%=h @exception.original_exception.class.to_s %> in - <%=h @request.parameters["controller"].capitalize if @request.parameters["controller"]%>#<%=h @request.parameters["action"] %> -

+<% @source_extract = @exception.source_extract(0, :html) %> +
+

+ <%=h @exception.original_exception.class.to_s %> in + <%=h @request.parameters["controller"].capitalize if @request.parameters["controller"]%>#<%=h @request.parameters["action"] %> +

+
-

- Showing <%=h @exception.file_name %> where line #<%=h @exception.line_number %> raised: -

<%=h @exception.message %>
-

+
+

+ Showing <%=h @exception.file_name %> where line #<%=h @exception.line_number %> raised: +

<%=h @exception.message %>
+

-

Extracted source (around line #<%=h @exception.line_number %>): -

<%=h @exception.source_extract %>

+
+
+

Extracted source (around line #<%=h @exception.line_number %>): +

+
+ + + + + +
+
+            <% @source_extract.keys.each do |line_number| %>
+<%= line_number -%>
+            <% end %>
+          
+
+
+<% @source_extract.each do |line, source| -%>
"><%= source -%>
<% end -%> +
+
+
+
-

<%=h @exception.sub_template_message %>

+

<%=h @exception.sub_template_message %>

-<%= render template: "rescues/_trace" %> -<%= render template: "rescues/_request_and_response" %> + <%= render template: "rescues/_trace" %> + <%= render template: "rescues/_request_and_response" %> +
+ diff --git a/actionpack/lib/action_view/template/error.rb b/actionpack/lib/action_view/template/error.rb index e00056781d..662e1ac66b 100644 --- a/actionpack/lib/action_view/template/error.rb +++ b/actionpack/lib/action_view/template/error.rb @@ -78,7 +78,7 @@ module ActionView end end - def source_extract(indentation = 0) + def source_extract(indentation = 0, output = :console) return unless num = line_number num = num.to_i @@ -88,12 +88,20 @@ module ActionView end_on_line = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min indent = end_on_line.to_s.size + indentation - line_counter = start_on_line return unless source_code = source_code[start_on_line..end_on_line] - source_code.sum do |line| + formatted_code_for(source_code, start_on_line, indent, output) + end + + def formatted_code_for(source_code, line_counter, indent, output) + start_value = (output == :html) ? {} : "" + source_code.inject(start_value) do |result, line| line_counter += 1 - "%#{indent}s: %s\n" % [line_counter, line] + if output == :html + result.update(line_counter.to_s => "%#{indent}s %s\n" % ["", line]) + else + result << "%#{indent}s: %s\n" % [line_counter, line] + end end end