template_error.rb 3.0 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3 4 5 6 7 8
module ActionView
  # The TemplateError exception is raised when the compilation of the template fails. This exception then gathers a
  # bunch of intimate details and uses it to report a very precise exception message.
  class TemplateError < ActionViewError #:nodoc:
    SOURCE_CODE_RADIUS = 3

    attr_reader :original_exception

9 10 11 12 13 14
    def initialize(base_path, file_path, assigns, source, original_exception)
      @base_path, @assigns, @source, @original_exception =
        base_path, assigns.dup, source, original_exception
      @file_path = file_path

      remove_deprecated_assigns!
D
Initial  
David Heinemeier Hansson 已提交
15
    end
16

D
Initial  
David Heinemeier Hansson 已提交
17
    def message
18 19 20 21 22
      ActiveSupport::Deprecation.silence { original_exception.message }
    end

    def clean_backtrace
      original_exception.clean_backtrace
D
Initial  
David Heinemeier Hansson 已提交
23
    end
24

D
Initial  
David Heinemeier Hansson 已提交
25 26 27 28 29 30 31 32 33
    def sub_template_message
      if @sub_templates
        "Trace of template inclusion: " +
        @sub_templates.collect { |template| strip_base_path(template) }.join(", ")
      else
        ""
      end
    end

34 35 36 37 38 39 40 41 42 43
    def source_extract(indentation = 0)
      return unless num = line_number
      num = num.to_i

      source_code = IO.readlines(@file_path)

      start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
      end_on_line   = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min

      indent = ' ' * indentation
D
Initial  
David Heinemeier Hansson 已提交
44
      line_counter = start_on_line
45 46

      source_code[start_on_line..end_on_line].sum do |line|
D
Initial  
David Heinemeier Hansson 已提交
47
        line_counter += 1
48
        "#{indent}#{line_counter}: #{line}"
D
Initial  
David Heinemeier Hansson 已提交
49 50
      end
    end
51

52
    def sub_template_of(template_path)
D
Initial  
David Heinemeier Hansson 已提交
53
      @sub_templates ||= []
54
      @sub_templates << template_path
D
Initial  
David Heinemeier Hansson 已提交
55
    end
56

D
Initial  
David Heinemeier Hansson 已提交
57
    def line_number
58 59 60 61 62
      @line_number ||=
        if file_name
          regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/

          $1 if message =~ regexp or clean_backtrace.find { |line| line =~ regexp }
63
        end
D
Initial  
David Heinemeier Hansson 已提交
64
    end
65

D
Initial  
David Heinemeier Hansson 已提交
66
    def file_name
67 68 69
      stripped = strip_base_path(@file_path)
      stripped.slice!(0,1) if stripped[0] == ?/
      stripped
D
Initial  
David Heinemeier Hansson 已提交
70
    end
71

D
Initial  
David Heinemeier Hansson 已提交
72
    def to_s
73 74
      "\n\n#{self.class} (#{message}) #{source_location}:\n" +
        "#{source_extract}\n    #{clean_backtrace.join("\n    ")}\n\n"
D
Initial  
David Heinemeier Hansson 已提交
75 76
    end

77
    def backtrace
78 79 80
      [
        "#{source_location.capitalize}\n\n#{source_extract(4)}\n    " +
        clean_backtrace.join("\n    ")
81 82 83
      ]
    end

D
Initial  
David Heinemeier Hansson 已提交
84
    private
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
      def remove_deprecated_assigns!
        ActionController::Base::DEPRECATED_INSTANCE_VARIABLES.each do |ivar|
          @assigns.delete(ivar)
        end
      end

      def strip_base_path(path)
        File.expand_path(path).
          gsub(/^#{Regexp.escape File.expand_path(RAILS_ROOT)}/, '').
          gsub(@base_path, "")
      end

      def source_location
        if line_number
          "on line ##{line_number} of "
        else
          'in '
        end + file_name
D
Initial  
David Heinemeier Hansson 已提交
103 104
      end
  end
105
end
106

107 108 109 110
if defined?(Exception::TraceSubstitutions)
  Exception::TraceSubstitutions << [/:in\s+`_run_(html|xml).*'\s*$/, '']
  Exception::TraceSubstitutions << [%r{^\s*#{Regexp.escape RAILS_ROOT}}, '#{RAILS_ROOT}'] if defined?(RAILS_ROOT)
end