Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_view/template_error.rb

Revision 9027, 3.1 kB (checked in by pratik, 6 months ago)

Handle template error gracefully when line number cannot be derived. Closes #11327 [Nex3]

Line 
1 module ActionView
2   # The TemplateError exception is raised when the compilation of the template fails. This exception then gathers a
3   # bunch of intimate details and uses it to report a very precise exception message.
4   class TemplateError < ActionViewError #:nodoc:
5     SOURCE_CODE_RADIUS = 3
6
7     attr_reader :original_exception
8
9     def initialize(template, assigns, original_exception)
10       @base_path = template.base_path_for_exception
11       @assigns, @source, @original_exception = assigns.dup, template.source, original_exception
12       @file_path = template.filename
13       @backtrace = compute_backtrace
14     end
15
16     def message
17       ActiveSupport::Deprecation.silence { original_exception.message }
18     end
19
20     def clean_backtrace
21       original_exception.clean_backtrace
22     end
23
24     def sub_template_message
25       if @sub_templates
26         "Trace of template inclusion: " +
27         @sub_templates.collect { |template| strip_base_path(template) }.join(", ")
28       else
29         ""
30       end
31     end
32
33     def source_extract(indentation = 0)
34       return unless num = line_number
35       num = num.to_i
36
37       source_code = IO.readlines(@file_path)
38
39       start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
40       end_on_line   = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min
41
42       indent = ' ' * indentation
43       line_counter = start_on_line
44       return unless source_code = source_code[start_on_line..end_on_line]
45      
46       source_code.sum do |line|
47         line_counter += 1
48         "#{indent}#{line_counter}: #{line}"
49       end
50     end
51
52     def sub_template_of(template_path)
53       @sub_templates ||= []
54       @sub_templates << template_path
55     end
56
57     def line_number
58       @line_number ||=
59         if file_name
60           regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/
61
62           $1 if message =~ regexp or clean_backtrace.find { |line| line =~ regexp }
63         end
64     end
65
66     def file_name
67       stripped = strip_base_path(@file_path)
68       stripped.slice!(0,1) if stripped[0] == ?/
69       stripped
70     end
71
72     def to_s
73       "\n\n#{self.class} (#{message}) #{source_location}:\n" +
74         "#{source_extract}\n    #{clean_backtrace.join("\n    ")}\n\n"
75     end
76
77     # don't do anything nontrivial here. Any raised exception from here becomes fatal
78     # (and can't be rescued).
79     def backtrace
80       @backtrace
81     end
82
83     private
84       def compute_backtrace
85         [
86           "#{source_location.capitalize}\n\n#{source_extract(4)}\n    " +
87           clean_backtrace.join("\n    ")
88         ]
89       end
90
91       def strip_base_path(path)
92         stripped_path = File.expand_path(path).gsub(@base_path, "")
93         stripped_path.gsub!(/^#{Regexp.escape File.expand_path(RAILS_ROOT)}/, '') if defined?(RAILS_ROOT)
94         stripped_path
95       end
96
97       def source_location
98         if line_number
99           "on line ##{line_number} of "
100         else
101           'in '
102         end + file_name
103       end
104   end
105 end
106
107 if defined?(Exception::TraceSubstitutions)
108   Exception::TraceSubstitutions << [/:in\s+`_run_(html|xml).*'\s*$/, '']
109   Exception::TraceSubstitutions << [%r{^\s*#{Regexp.escape RAILS_ROOT}/}, ''] if defined?(RAILS_ROOT)
110 end
Note: See TracBrowser for help on using the browser.